[英]Change output of only the first line in bash script
Here is my source:这是我的来源:
TABLE;APGFPOLI;
And here is the output that i want:这是我想要的 output:
01 APGFPOLI.
So what i am trying to do is remove "TABLE" and add 01 before " APGFPOLI ".所以我想做的是删除“TABLE”并在“ APGFPOLI ”之前添加 01。 I need to do it for the first line only.我只需要为第一行做。 So i tried to do:所以我试着做:
#!/bin/bash
#Fichier Source
fichier="APGFPOLI.des.txt"
champAdd="05 "
if [[ -f "$fichier" ]]
then
# read it
sed -i '1 s/TABLE//' $fichier |sed -i 's/CHAR/PIC X/' $fichier | sed -E '/Numérique/s/;Numérique\s+([^;]*)/;PIC 9(\1)/' $fichier | while IFS=';' read -r nomChamp format libelle
do
echo \* $libelle
echo $champAdd $nomChamp $format.
done > test.txt
fi
As you can see, my first sed is supposed to remove TABLE but it dont work.如您所见,我的第一个 sed 应该删除 TABLE 但它不起作用。 i also do a echo for my others line but i'd like to echo this specific first line too.我也为我的其他线路做一个回应,但我也想回应这个特定的第一行。
Here is the output my bash gives me:这是 output 我的 bash 给我的:
*
05 TABLE APGFPOLI.
Here is my full source if it helps:如果有帮助,这是我的完整来源:
TABLE;APGFPOLI;
Contrat;CHAR(16);Numéro du contrat
Libelle;CHAR(30);Libellé du contrat
DtCreation;CHAR(8);Date de création
DtMaj;CHAR(8);Date de dernière MAJ
DtEffet;CHAR(8);Date d'effet adhésion
MotifAdh;CHAR(2);Motif d'adhésion
DtRadiation;CHAR(8);Date de radiation
DtEnrRad;CHAR(8);Date enregistrement radiat
MotifRad;CHAR(2);Motif de radiation
MtPrime;Numérique 8.2;Montant prime d'origine
DtEffetSusp;CHAR(8);Date d'effet de suspension
DtFinSusp;CHAR(8);Date de fin de suspension
MotifSusp;CHAR(2);Motif de suspension
DestBord;CHAR(1);Destinataire du bordereau
CdDest;CHAR(5);Code du destinataire
NivRupBord;CHAR(1);Niveau rupture bordereau
BordCETIP;CHAR(1);Bordereau CTIP
EnvBordNom;CHAR(1);Envoi bordereau nominatif
Indice;CHAR(2);Indice appliqué
Echeance;CHAR(2);Echéance de l'indice (MM)
Effectif;CHAR(5);Effectif
CdRegr;CHAR(3);Code regroupement 1
CdGroupe;CHAR(3);Code regroupement 2
Periodicite;CHAR(1);Périodicité
Terme;CHAR(1);Terme
Produit;CHAR(6);Code produit affecté
Inspecteur;CHAR(5);Inspecteur
CleInsp;CHAR(1);Clé inspecteur
Filler;CHAR(6);Filler
You generally don't want to run sed -i
on the same file more than once.您通常不想在同一个文件上多次运行sed -i
。
Your entire task can be rephrased into just您的整个任务可以改写为
sed -i '1s/TABLE/01 /' APGFPOLI.des.txt
If the replacement string should come from a shell variable, you need to use double quotes instead of single:如果替换字符串应该来自 shell 变量,则需要使用双引号而不是单引号:
replacement="05"
sed -i "1s/TABLE/$replacement /" APGFPOLI.des.txt
If you want to keep your other tasks (which are not documented in the question) you can easily merge them into the same script.如果您想保留其他任务(问题中没有记录),您可以轻松地将它们合并到同一个脚本中。 Remember, sed
is a scripting language;请记住, sed
是一种脚本语言; you can feed in arbitrarily complex scripts (and some crazy people have even implemented desk calculators and Game of Life in sed
scripts).您可以输入任意复杂的脚本(一些疯狂的人甚至在sed
脚本中实现了桌面计算器和生命游戏)。
sed -i -E -e '1 s/TABLE/01 /' -e 's/CHAR/PIC X/' \
-e '/Numérique/s/;Numérique\s+([^;]*)/;PIC 9(\1)/' APGFPOLI.des.txt
If after this you want to pull out the result and display it with some decorations, I would add a second sed
script without -i
so that it displays the output on standard output without modifying the file.如果在此之后您想拉出结果并用一些装饰显示它,我将添加第二个不带-i
的sed
脚本,以便它在标准 Z78E6221F6393D1356681DB398F14CE6 上显示 output 修改文件。
sed '1s/\([^;]*\);\([^;]*\);\([^;]*\);.*/* \3\n05 \2 \1/;q' APGFPOLI.des.txt
For removing TABLE;
用于删除TABLE;
from the first line and to prepend 01
, you just have to do:从第一行开始并添加01
,您只需要:
fichier="APGFPOLI.des.txt"
sed -i '1s/^TABLE;/01 /' "$fichier"
When you ask something in SO, it is advisable to remove the part of the code that is not relevant to your problem, like the command sed -i 's/CHAR/PIC X/' $fichier
and so on.当您在 SO 中询问某些内容时,建议删除与您的问题无关的代码部分,例如命令sed -i 's/CHAR/PIC X/' $fichier
等。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.