I have a text file containing a suite of jobs that look like the below job. What I would like to do is move the JOB_NAME onto the delete_job: line for each job to look like
delete_job: JOB_NAME
I have tried many ways but can't get it to work! Any ideas how this can be achieved?
delete_job:
JOB_NAME
job_type: BOX
description: "*******"
date_conditions: 0
owner: *******
alarm_if_fail: 1
EDIT2: As per ED sir's suggestion adding 1 more solution too now.
awk 'val{print val OFS $0;val="";next}/delete_job/||/update_job/||/insert_job/{val=$0;next} 1' Input_file
EDIT: as OP told many strings could be there to search so changing accordingly now.
awk '/delete_job/||/update_job/||/insert_job/{val=$0;getline;print val OFS $0;next} 1' Input_file
Could you please try following and let me know if this helps you.
awk '/delete_job/{val=$0;getline;print val OFS $0;next} 1' Input_file
With good old ed:
ed infile <<'EOE'
g/^delete_job:$/ s/$/ /\
.,+j
wq
EOE
This collects all lines that match ^delete_job:$ with the g// global command; s/$/ / appends a space to that line, and .,+j joins it with the next line before wq writes the buffer back to the file and exits.
A sed solution:
sed '/^delete_job:$/{N;s/\n/ /;}' filename
The right way to do this with awk (assuming multiple possible "action_job" lines) is to just save the "action_job" line when you see it and then print it as a prefix when you're printing the next line:
awk '/(delete|update|insert)_job/{act=$0 OFS; next} {print act $0; act=""}'
I'd use awk, perhaps something like
<yourfile awk '
BEGIN { output = 1 }
/delete_job:/ { output = 0; next; }
output == 0 { print "delete_job: " $0; output = 1; next; }
output == 1 { print }' > newfile
Maybe that helps. The result will be written to newfile.
two other awks
$ awk '/^delete_job:/{printf "%s", $0 OFS; next}1' file
or
$ awk '{ORS=/^delete_job:/?OFS:RS}1' file
With paste, head and tail:
$ (head -n2 | paste -d' ' -s; tail -n+1) < filename.txt
Related
I have a file such as:
Colcht.WP_006104309.1
Moopro.WP_070396948.1
Mastes.WP_027845098.1
Phowil.WP_068791039.1
Cyaapo.WP_015218744.1
Gemher.WP_017295578.1
Lyncon.WP_039726659.1
Glokil.WP_023171831.1
Noscya.WP_087539356.1
Photen.WP_073607805.1
Hydriv.WP_073598454.1
Lepoha.WP_088893428.1
Nodnod.WP_017300904.1
Noscya.WP_087540001.1
Spisub.WP_017307136.1
Scy0HK.WP_073635112.1
PlaSR0.WP_054467905.1
I used grep -A 1 -F -f to grep from another file, information pertaining to each of the lines in the text file above. A subset of the outcome is as follows:
>Cyaapo.WP_015218744.1
MNIVVVGLSHKTAAVEVREKLSIPEAKIEDSIRHLLTYPHIEEVAIISTCNRLEIYAVVKETEQGVKEITQFLAEIGNLALLELRRHLFILLHQDAIRHLMRVAAGLESLVLGEGQILAQVKTTHKLGMKYNGMSRLLDRLFKQAISAGKRVRTETNIGTGAVSISSAAVELVDTKIEDLSSQKVTIIGAGKMSRLLVQHLLAKGVEDIIIVNRSHNRSQELAKQFPQANLKLNLLEDMMTMVAQSDIVFTSTGATQPILDKNNLSSLSINHSLMLVDISVPRNVASDVTELEFIKSYNVDDLKAVVAQNHASRREMAREAENLLEEEIEAFELWWQSLETVPTISCLRSKIEQIREQELEKALSRLGSEFAEKHQEVIEALTRGIVNKILHDPMVQLRTQKDIEARRKALHTLQTLFDLEVSEQLI
>Gemher.WP_017295578.1
MNIIVVGLSHKTAAVEVREKLSIPEAKIEESIKHLLSYPHIEEVGIISTCNRLEIYAVVKETEQGVKEITQFLAEIGHLSLHSLRRHLFILLHQDAIRHLMRVSAGLESLVLGEGQILAQVKNTHKLSTKYQGMGRLLDRLFKQAMSAGKRVRTETNIGTGAVSISSAAVELVDMKLDDLSRQKVSIIGAGKMSRLLVQHLISKGVSDITIVNRSVSRSKELAKQFPQIELKLNLLEEMMEIVRDSDIVFTSTGATQPILDKNNLCSIECHHSIMLVDISVPRNVASDVEELDFIVAYNVDDLKAVVAQNQASRREMAREAELLLEEEIEAFELWWQSLETVPTISCLRSKIEEIREQELEKALSRLGSEFAEKHQEVIEALTRGIVNKILHDPMVQLRSQQDIEARRKALQTLQTLFNLEISEQFG
>Glokil.WP_023171831.1
MQIAVIGLSHRTAPVEIREKVSIPEQQVAEYVSRLRSCSQIAECAILSTCNRLEIYAVLRDSEHGLREVTQFLAESKGVALPMLQRHLFTLLHQDAVMHLMRVAAGLDSIVLGEGQILAQIKVTHKLAQQGKGVDRILNQLFKAAITGGKRVREETDIGKGAISVSSAAVEMAMRKKNRRSLQDQRCLVVGAGKMGELVLRHLISKGARQIIVLNRSLEKAAQMVEQFGLMLPVATIDELGNHLGAADLIFTCTSASEPLINYERLSQVRREQPLMIFDIAVPRNVAVDVEELSNVHLFNVDHLKQVVEENRAYRQLMVQQCEDILLQQLDEFLDWWRNLEAVPTINSLRQKVETIREQELEKALSRLGTEFGEKHQGIIDSLTRAIVNKILHDPMVQLRAQRDVEARRRALQTLQTLFNLEPLGSNPEPPVL
>Hydriv.WP_073598454.1
MNIAVVGLSHKTAPVEIREKLSIQEAKLESALAHLRSYPHIIEVAIISTCNRLEIYAIATETDQGVREISQFLSEIGHIPLDRLRRYLFILLHQDAVRHLMRVAAGLESLVLGEGQILAQVKNTHKLAQKYQSLGQILDRLFKQAMTAGKRVRSETNIGTGAVSISSAAVELAHMKAENLAARRVCIIGAGKMSRLLVQHLLAKGTQQICIVNRSHRRAEELASQFPEVQLKLYPLTEMMSAVAASDIVFTSTAATEPIINRSQLEASLTRDRELMLFDISVPRNVHADVGGMESVQSYNVDDLKAVVAQNYESRRKMAQEAEALLEEEIAAFELWWRSLETVPTISCLRSKVETIREQELEKALSRLGTEFAEKHQEVIEALTRGIVNKILHEPMVQLRAQQDIEARRRCLQSLQMLFNLEIEKQVI
>Lepoha.WP_088893428.1
MNIVVVGLSHRTAPVEVREKLSIPTPQMEAAIAHLRSFPHIEEATILSTCNRLEVYVVTSETEQGVREVTQFLSEYGKISVSQLRPYLFILLHDDAVMHLMRVSAGLDSLVLGEGQILAQVKHTHKVGQQYNGIGRILNRLFNQAITAGKRVRTETSIGTGAVSISSAAVELAQLKVQHLPACRVAILGAGKMSRLVVQHLISKGATQICIVNRSLDSARELAQQFKEAEIRLHLLDEMMHVICNSDLVFTATAATEPLIDRAKLESTIDPLHSLKLFDISVPRNVHADVNELDHVQLFNVDDLKAVVAQNQESRRQMALEAENILDEEVAAFDLWWRSLETVATISELRDKVEAIRAQELEKALSRLGSEFAEKHQEVIEALTRGIVNKILHDPMVQLRAQQDIEARRRAMQTLRSLFNLEEPASNKA
However, as you can see, the order in the first file (1st: Colcht.WP_006104309.1, 2nd: Moopro.WP_070396948.1, 3rd: Mastes.WP_027845098.1) is not longer respected.
A subset of the expected output would be:
>Colcht.WP_006104309.1
MENYNTSNIDNVLLLKGDDIINLFKNREQDILDLVKLTYKIHGRGDSTLPHSSFLRFPDKNKERIIALPAYLGGEINTAGIKWIASFPGNLARGMERASAILIINSTETGRPQAIMEGSVISAKRTAASAALAAHFLRDRQSLVTVGLIGCGLINFETVRFLLKVRPEIETLFLYDVSLEKSDQFKRKCQQLSQNRELVILDNPDDVFKHSSVIALATTASQPHIVDISACQSDSIILHTSLRDLSPEIILSVDNIVDDIDHVCRAQTSIHLAEQKTGNRDFIRCPLSDILNGVAAPRQNNSQIAVFSPFGLGVLDLALGQLAYQLADETNVGTRLTSFFPVSWLQREDE
>Moopro.WP_070396948.1
METAYQGFAQQQPGDVIVLSASDILSLLAGREKELIEVVRQTYIAHARGESALPPSPFLRFANHPKNRIIAKPAYLGESFETAGIKWISSFPDNYQFGLLRASAVIILNSVKTGFAEAILEGSVISAKRTAASAALAARLLQSETQPESIGIIACGVINFEITRFLLAEFPTVKNLVIFDIDHERAVQYKSRCETNFETPNITIANDINTVLSSTSIISIATTETTPHIFEISACQPGSNILHISLRDFSPEVILSCDNIVDDVEHICSAQTSVHLAEQKINHRHFIRGSIGDILCGKILAKPTPSAITIFSPFGLGILDLAVAKLVHEWGIARNLGTVIPSFGCLPHE
>Mastes.WP_027845098.1
MSNKHHLSFTYLSQEDLLDAGCFDIRMVMDIAEKAMLEFERHHVIFPEKIVQIFNQATQERINCLPATLLDEKVCGVKWVSVFPMNPIEHDQQNLSAIFILSEIETGFPICVMEGTLASNMRVAAIGGLAAKYLARQDSEVIGFIGAGEQAKMHLIAMKAVCPSLKQCRVAAHVVKHEEQFIAELSRLYPEMEFVSTNTNLQKAIEDADILVTATSAQAPLLKATWVKPGTFYSHIGGWEDEFEVALQADKIVCDDWETVTHRTQTLSRMYQEGLINANNIHADLHELVSGKKAGRESQTERIYFNAVGLAYIDIAIAMAMFNRAREKQKGTQLDLQQSMVFEHLGLKSKVKL
>Phowil.WP_068791039.1
MRVISAAEVQAALDFETLVGRLRDIFRRGGEAPARQQYDIAITGEPAQTLLLAPAWQAGRHVGVQIATVTPGNGARGLPAGMGAYLLLDGRSGAPAALIDGPMLTLRRTAAASALASAYLSRPDSARLLMVGTGALAPHLIAAHAAVRPIREVLVWGRTPAKAARLAKAVKLPRVRLAWTEDLEGAVRGADIVACATLSQQPLLRGAWLRPGQHLDLVGAYRPEMRESDGEVFRRARVFVDTRAGALAEAGDLIQALAEGALSAADVAADLFELARGEKAGRRFYDQITLFKATGSALEDLAAAQLTVERA
>Cyaapo.WP_015218744.1
MNIVVVGLSHKTAAVEVREKLSIPEAKIEDSIRHLLTYPHIEEVAIISTCNRLEIYAVVKETEQGVKEITQFLAEIGNLALLELRRHLFILLHQDAIRHLMRVAAGLESLVLGEGQILAQVKTTHKLGMKYNGMSRLLDRLFKQAISAGKRVRTETNIGTGAVSISSAAVELVDTKIEDLSSQKVTIIGAGKMSRLLVQHLLAKGVEDIIIVNRSHNRSQELAKQFPQANLKLNLLEDMMTMVAQSDIVFTSTGATQPILDKNNLSSLSINHSLMLVDISVPRNVASDVTELEFIKSYNVDDLKAVVAQNHASRREMAREAENLLEEEIEAFELWWQSLETVPTISCLRSKIEQIREQELEKALSRLGSEFAEKHQEVIEALTRGIVNKILHDPMVQLRTQKDIEARRKALHTLQTLFDLEVSEQLI
Does anyone know how I can grep as I did above, put keeping the order from the first file?
Any help is extremely appreciated :)
If you want to keep the order of the first file, it's better to parse the second one, store the next lines, and then parse the first.
> cat tst.awk
FNR==NR && p {
a[prev]=$0
p=0
next
}
FNR==NR && $0~/^>/ {
prev=substr($0,2)
p=1
next
}
$0 in a {
print ">" $0 RS a[$0]
}
Usage:
awk -f tst.awk file2 file1
In case file2 is huge and you don't have enough memory, you could just replace file2 with the output of your grep command (only interesting part of file2).
awk -f tst.awk <(grep -A 1 -f file1 file2) file1
or else, you can still pass file1 file2 but you have to save the order of the rows and do the job in the END section.
> cat tst.awk
FNR==NR {
row[NR]=$0
a[$0]
next
}
p {
next_row[x]=$0
p=0
next
}
substr($0,2) in a {
x=substr($0,2)
p=1
}
END {
for (i=1;i in row;i++)
if (next_row[row[i]])
print ">" row[i] RS next_row[row[i]]
}
Usage:
awk -f tst.awk file1 file2
$ awk '
NR==FNR { if (/^>/) tag=$1; else a[substr(tag,2)]=tag ORS $0; next }
$1 in a { print a[$1] }
' file2 file1
>Colcht.WP_006104309.1
MENYNTSNIDNVLLLKGDDIINLFKNREQDILDLVKLTYKIHGRGDSTLPHSSFLRFPDKNKERIIALPAYLGGEINTAGIKWIASFPGNLARGMERASAILIINSTETGRPQAIMEGSVISAKRTAASAALAAHFLRDRQSLVTVGLIGCGLINFETVRFLLKVRPEIETLFLYDVSLEKSDQFKRKCQQLSQNRELVILDNPDDVFKHSSVIALATTASQPHIVDISACQSDSIILHTSLRDLSPEIILSVDNIVDDIDHVCRAQTSIHLAEQKTGNRDFIRCPLSDILNGVAAPRQNNSQIAVFSPFGLGVLDLALGQLAYQLADETNVGTRLTSFFPVSWLQREDE
>Moopro.WP_070396948.1
METAYQGFAQQQPGDVIVLSASDILSLLAGREKELIEVVRQTYIAHARGESALPPSPFLRFANHPKNRIIAKPAYLGESFETAGIKWISSFPDNYQFGLLRASAVIILNSVKTGFAEAILEGSVISAKRTAASAALAARLLQSETQPESIGIIACGVINFEITRFLLAEFPTVKNLVIFDIDHERAVQYKSRCETNFETPNITIANDINTVLSSTSIISIATTETTPHIFEISACQPGSNILHISLRDFSPEVILSCDNIVDDVEHICSAQTSVHLAEQKINHRHFIRGSIGDILCGKILAKPTPSAITIFSPFGLGILDLAVAKLVHEWGIARNLGTVIPSFGCLPHE
>Mastes.WP_027845098.1
MSNKHHLSFTYLSQEDLLDAGCFDIRMVMDIAEKAMLEFERHHVIFPEKIVQIFNQATQERINCLPATLLDEKVCGVKWVSVFPMNPIEHDQQNLSAIFILSEIETGFPICVMEGTLASNMRVAAIGGLAAKYLARQDSEVIGFIGAGEQAKMHLIAMKAVCPSLKQCRVAAHVVKHEEQFIAELSRLYPEMEFVSTNTNLQKAIEDADILVTATSAQAPLLKATWVKPGTFYSHIGGWEDEFEVALQADKIVCDDWETVTHRTQTLSRMYQEGLINANNIHADLHELVSGKKAGRESQTERIYFNAVGLAYIDIAIAMAMFNRAREKQKGTQLDLQQSMVFEHLGLKSKVKL
>Phowil.WP_068791039.1
MRVISAAEVQAALDFETLVGRLRDIFRRGGEAPARQQYDIAITGEPAQTLLLAPAWQAGRHVGVQIATVTPGNGARGLPAGMGAYLLLDGRSGAPAALIDGPMLTLRRTAAASALASAYLSRPDSARLLMVGTGALAPHLIAAHAAVRPIREVLVWGRTPAKAARLAKAVKLPRVRLAWTEDLEGAVRGADIVACATLSQQPLLRGAWLRPGQHLDLVGAYRPEMRESDGEVFRRARVFVDTRAGALAEAGDLIQALAEGALSAADVAADLFELARGEKAGRRFYDQITLFKATGSALEDLAAAQLTVERA
>Cyaapo.WP_015218744.1
MNIVVVGLSHKTAAVEVREKLSIPEAKIEDSIRHLLTYPHIEEVAIISTCNRLEIYAVVKETEQGVKEITQFLAEIGNLALLELRRHLFILLHQDAIRHLMRVAAGLESLVLGEGQILAQVKTTHKLGMKYNGMSRLLDRLFKQAISAGKRVRTETNIGTGAVSISSAAVELVDTKIEDLSSQKVTIIGAGKMSRLLVQHLLAKGVEDIIIVNRSHNRSQELAKQFPQANLKLNLLEDMMTMVAQSDIVFTSTGATQPILDKNNLSSLSINHSLMLVDISVPRNVASDVTELEFIKSYNVDDLKAVVAQNHASRREMAREAENLLEEEIEAFELWWQSLETVPTISCLRSKIEQIREQELEKALSRLGSEFAEKHQEVIEALTRGIVNKILHDPMVQLRTQKDIEARRKALHTLQTLFDLEVSEQLI
>Gemher.WP_017295578.1
MNIIVVGLSHKTAAVEVREKLSIPEAKIEESIKHLLSYPHIEEVGIISTCNRLEIYAVVKETEQGVKEITQFLAEIGHLSLHSLRRHLFILLHQDAIRHLMRVSAGLESLVLGEGQILAQVKNTHKLSTKYQGMGRLLDRLFKQAMSAGKRVRTETNIGTGAVSISSAAVELVDMKLDDLSRQKVSIIGAGKMSRLLVQHLISKGVSDITIVNRSVSRSKELAKQFPQIELKLNLLEEMMEIVRDSDIVFTSTGATQPILDKNNLCSIECHHSIMLVDISVPRNVASDVEELDFIVAYNVDDLKAVVAQNQASRREMAREAELLLEEEIEAFELWWQSLETVPTISCLRSKIEEIREQELEKALSRLGSEFAEKHQEVIEALTRGIVNKILHDPMVQLRSQQDIEARRKALQTLQTLFNLEISEQFG
>Glokil.WP_023171831.1
MQIAVIGLSHRTAPVEIREKVSIPEQQVAEYVSRLRSCSQIAECAILSTCNRLEIYAVLRDSEHGLREVTQFLAESKGVALPMLQRHLFTLLHQDAVMHLMRVAAGLDSIVLGEGQILAQIKVTHKLAQQGKGVDRILNQLFKAAITGGKRVREETDIGKGAISVSSAAVEMAMRKKNRRSLQDQRCLVVGAGKMGELVLRHLISKGARQIIVLNRSLEKAAQMVEQFGLMLPVATIDELGNHLGAADLIFTCTSASEPLINYERLSQVRREQPLMIFDIAVPRNVAVDVEELSNVHLFNVDHLKQVVEENRAYRQLMVQQCEDILLQQLDEFLDWWRNLEAVPTINSLRQKVETIREQELEKALSRLGTEFGEKHQGIIDSLTRAIVNKILHDPMVQLRAQRDVEARRRALQTLQTLFNLEPLGSNPEPPVL
>Hydriv.WP_073598454.1
MNIAVVGLSHKTAPVEIREKLSIQEAKLESALAHLRSYPHIIEVAIISTCNRLEIYAIATETDQGVREISQFLSEIGHIPLDRLRRYLFILLHQDAVRHLMRVAAGLESLVLGEGQILAQVKNTHKLAQKYQSLGQILDRLFKQAMTAGKRVRSETNIGTGAVSISSAAVELAHMKAENLAARRVCIIGAGKMSRLLVQHLLAKGTQQICIVNRSHRRAEELASQFPEVQLKLYPLTEMMSAVAASDIVFTSTAATEPIINRSQLEASLTRDRELMLFDISVPRNVHADVGGMESVQSYNVDDLKAVVAQNYESRRKMAQEAEALLEEEIAAFELWWRSLETVPTISCLRSKVETIREQELEKALSRLGTEFAEKHQEVIEALTRGIVNKILHEPMVQLRAQQDIEARRRCLQSLQMLFNLEIEKQVI
>Lepoha.WP_088893428.1
MNIVVVGLSHRTAPVEVREKLSIPTPQMEAAIAHLRSFPHIEEATILSTCNRLEVYVVTSETEQGVREVTQFLSEYGKISVSQLRPYLFILLHDDAVMHLMRVSAGLDSLVLGEGQILAQVKHTHKVGQQYNGIGRILNRLFNQAITAGKRVRTETSIGTGAVSISSAAVELAQLKVQHLPACRVAILGAGKMSRLVVQHLISKGATQICIVNRSLDSARELAQQFKEAEIRLHLLDEMMHVICNSDLVFTATAATEPLIDRAKLESTIDPLHSLKLFDISVPRNVHADVNELDHVQLFNVDDLKAVVAQNQESRRQMALEAENILDEEVAAFDLWWRSLETVATISELRDKVEAIRAQELEKALSRLGSEFAEKHQEVIEALTRGIVNKILHDPMVQLRAQQDIEARRRAMQTLRSLFNLEEPASNKA
The above was run on these input files:
$ head -n 50 file*
==> file1 <==
Colcht.WP_006104309.1
Moopro.WP_070396948.1
Mastes.WP_027845098.1
Phowil.WP_068791039.1
Cyaapo.WP_015218744.1
Gemher.WP_017295578.1
Lyncon.WP_039726659.1
Glokil.WP_023171831.1
Noscya.WP_087539356.1
Photen.WP_073607805.1
Hydriv.WP_073598454.1
Lepoha.WP_088893428.1
Nodnod.WP_017300904.1
Noscya.WP_087540001.1
Spisub.WP_017307136.1
Scy0HK.WP_073635112.1
PlaSR0.WP_054467905.1
==> file2 <==
>Cyaapo.WP_015218744.1
MNIVVVGLSHKTAAVEVREKLSIPEAKIEDSIRHLLTYPHIEEVAIISTCNRLEIYAVVKETEQGVKEITQFLAEIGNLALLELRRHLFILLHQDAIRHLMRVAAGLESLVLGEGQILAQVKTTHKLGMKYNGMSRLLDRLFKQAISAGKRVRTETNIGTGAVSISSAAVELVDTKIEDLSSQKVTIIGAGKMSRLLVQHLLAKGVEDIIIVNRSHNRSQELAKQFPQANLKLNLLEDMMTMVAQSDIVFTSTGATQPILDKNNLSSLSINHSLMLVDISVPRNVASDVTELEFIKSYNVDDLKAVVAQNHASRREMAREAENLLEEEIEAFELWWQSLETVPTISCLRSKIEQIREQELEKALSRLGSEFAEKHQEVIEALTRGIVNKILHDPMVQLRTQKDIEARRKALHTLQTLFDLEVSEQLI
>Gemher.WP_017295578.1
MNIIVVGLSHKTAAVEVREKLSIPEAKIEESIKHLLSYPHIEEVGIISTCNRLEIYAVVKETEQGVKEITQFLAEIGHLSLHSLRRHLFILLHQDAIRHLMRVSAGLESLVLGEGQILAQVKNTHKLSTKYQGMGRLLDRLFKQAMSAGKRVRTETNIGTGAVSISSAAVELVDMKLDDLSRQKVSIIGAGKMSRLLVQHLISKGVSDITIVNRSVSRSKELAKQFPQIELKLNLLEEMMEIVRDSDIVFTSTGATQPILDKNNLCSIECHHSIMLVDISVPRNVASDVEELDFIVAYNVDDLKAVVAQNQASRREMAREAELLLEEEIEAFELWWQSLETVPTISCLRSKIEEIREQELEKALSRLGSEFAEKHQEVIEALTRGIVNKILHDPMVQLRSQQDIEARRKALQTLQTLFNLEISEQFG
>Glokil.WP_023171831.1
MQIAVIGLSHRTAPVEIREKVSIPEQQVAEYVSRLRSCSQIAECAILSTCNRLEIYAVLRDSEHGLREVTQFLAESKGVALPMLQRHLFTLLHQDAVMHLMRVAAGLDSIVLGEGQILAQIKVTHKLAQQGKGVDRILNQLFKAAITGGKRVREETDIGKGAISVSSAAVEMAMRKKNRRSLQDQRCLVVGAGKMGELVLRHLISKGARQIIVLNRSLEKAAQMVEQFGLMLPVATIDELGNHLGAADLIFTCTSASEPLINYERLSQVRREQPLMIFDIAVPRNVAVDVEELSNVHLFNVDHLKQVVEENRAYRQLMVQQCEDILLQQLDEFLDWWRNLEAVPTINSLRQKVETIREQELEKALSRLGTEFGEKHQGIIDSLTRAIVNKILHDPMVQLRAQRDVEARRRALQTLQTLFNLEPLGSNPEPPVL
>Hydriv.WP_073598454.1
MNIAVVGLSHKTAPVEIREKLSIQEAKLESALAHLRSYPHIIEVAIISTCNRLEIYAIATETDQGVREISQFLSEIGHIPLDRLRRYLFILLHQDAVRHLMRVAAGLESLVLGEGQILAQVKNTHKLAQKYQSLGQILDRLFKQAMTAGKRVRSETNIGTGAVSISSAAVELAHMKAENLAARRVCIIGAGKMSRLLVQHLLAKGTQQICIVNRSHRRAEELASQFPEVQLKLYPLTEMMSAVAASDIVFTSTAATEPIINRSQLEASLTRDRELMLFDISVPRNVHADVGGMESVQSYNVDDLKAVVAQNYESRRKMAQEAEALLEEEIAAFELWWRSLETVPTISCLRSKVETIREQELEKALSRLGTEFAEKHQEVIEALTRGIVNKILHEPMVQLRAQQDIEARRRCLQSLQMLFNLEIEKQVI
>Lepoha.WP_088893428.1
MNIVVVGLSHRTAPVEVREKLSIPTPQMEAAIAHLRSFPHIEEATILSTCNRLEVYVVTSETEQGVREVTQFLSEYGKISVSQLRPYLFILLHDDAVMHLMRVSAGLDSLVLGEGQILAQVKHTHKVGQQYNGIGRILNRLFNQAITAGKRVRTETSIGTGAVSISSAAVELAQLKVQHLPACRVAILGAGKMSRLVVQHLISKGATQICIVNRSLDSARELAQQFKEAEIRLHLLDEMMHVICNSDLVFTATAATEPLIDRAKLESTIDPLHSLKLFDISVPRNVHADVNELDHVQLFNVDDLKAVVAQNQESRRQMALEAENILDEEVAAFDLWWRSLETVATISELRDKVEAIRAQELEKALSRLGSEFAEKHQEVIEALTRGIVNKILHDPMVQLRAQQDIEARRRAMQTLRSLFNLEEPASNKA
>Colcht.WP_006104309.1
MENYNTSNIDNVLLLKGDDIINLFKNREQDILDLVKLTYKIHGRGDSTLPHSSFLRFPDKNKERIIALPAYLGGEINTAGIKWIASFPGNLARGMERASAILIINSTETGRPQAIMEGSVISAKRTAASAALAAHFLRDRQSLVTVGLIGCGLINFETVRFLLKVRPEIETLFLYDVSLEKSDQFKRKCQQLSQNRELVILDNPDDVFKHSSVIALATTASQPHIVDISACQSDSIILHTSLRDLSPEIILSVDNIVDDIDHVCRAQTSIHLAEQKTGNRDFIRCPLSDILNGVAAPRQNNSQIAVFSPFGLGVLDLALGQLAYQLADETNVGTRLTSFFPVSWLQREDE
>Moopro.WP_070396948.1
METAYQGFAQQQPGDVIVLSASDILSLLAGREKELIEVVRQTYIAHARGESALPPSPFLRFANHPKNRIIAKPAYLGESFETAGIKWISSFPDNYQFGLLRASAVIILNSVKTGFAEAILEGSVISAKRTAASAALAARLLQSETQPESIGIIACGVINFEITRFLLAEFPTVKNLVIFDIDHERAVQYKSRCETNFETPNITIANDINTVLSSTSIISIATTETTPHIFEISACQPGSNILHISLRDFSPEVILSCDNIVDDVEHICSAQTSVHLAEQKINHRHFIRGSIGDILCGKILAKPTPSAITIFSPFGLGILDLAVAKLVHEWGIARNLGTVIPSFGCLPHE
>Mastes.WP_027845098.1
MSNKHHLSFTYLSQEDLLDAGCFDIRMVMDIAEKAMLEFERHHVIFPEKIVQIFNQATQERINCLPATLLDEKVCGVKWVSVFPMNPIEHDQQNLSAIFILSEIETGFPICVMEGTLASNMRVAAIGGLAAKYLARQDSEVIGFIGAGEQAKMHLIAMKAVCPSLKQCRVAAHVVKHEEQFIAELSRLYPEMEFVSTNTNLQKAIEDADILVTATSAQAPLLKATWVKPGTFYSHIGGWEDEFEVALQADKIVCDDWETVTHRTQTLSRMYQEGLINANNIHADLHELVSGKKAGRESQTERIYFNAVGLAYIDIAIAMAMFNRAREKQKGTQLDLQQSMVFEHLGLKSKVKL
>Phowil.WP_068791039.1
MRVISAAEVQAALDFETLVGRLRDIFRRGGEAPARQQYDIAITGEPAQTLLLAPAWQAGRHVGVQIATVTPGNGARGLPAGMGAYLLLDGRSGAPAALIDGPMLTLRRTAAASALASAYLSRPDSARLLMVGTGALAPHLIAAHAAVRPIREVLVWGRTPAKAARLAKAVKLPRVRLAWTEDLEGAVRGADIVACATLSQQPLLRGAWLRPGQHLDLVGAYRPEMRESDGEVFRRARVFVDTRAGALAEAGDLIQALAEGALSAADVAADLFELARGEKAGRRFYDQITLFKATGSALEDLAAAQLTVERA
>Cyaapo.WP_015218744.1
MNIVVVGLSHKTAAVEVREKLSIPEAKIEDSIRHLLTYPHIEEVAIISTCNRLEIYAVVKETEQGVKEITQFLAEIGNLALLELRRHLFILLHQDAIRHLMRVAAGLESLVLGEGQILAQVKTTHKLGMKYNGMSRLLDRLFKQAISAGKRVRTETNIGTGAVSISSAAVELVDTKIEDLSSQKVTIIGAGKMSRLLVQHLLAKGVEDIIIVNRSHNRSQELAKQFPQANLKLNLLEDMMTMVAQSDIVFTSTGATQPILDKNNLSSLSINHSLMLVDISVPRNVASDVTELEFIKSYNVDDLKAVVAQNHASRREMAREAENLLEEEIEAFELWWQSLETVPTISCLRSKIEQIREQELEKALSRLGSEFAEKHQEVIEALTRGIVNKILHDPMVQLRTQKDIEARRKALHTLQTLFDLEVSEQLI
Could you please try following, written with shown samples only. Where Input_file2 is the file which has line starts from > in it. I am yet to test it should work but.
awk '
FNR==NR{
a[$0]
next
}
/^>/ && (substr($0,2) in a){
found=1
val=$0
next
}
found!=""{
print val ORS $0
found=""
}' Input_file1 Input_file2
Explanation: Adding detailed explanation for above.
awk ' ##starting awk program from here.
FNR==NR{ ##Checking condition if FNR==NR which is true when Input_file1 is being read.
a[$0] ##Creating array a with index of current line here.
next ##next will skip all further statements from here.
} ##Closing FNR==NR block here.
/^>/ && (substr($0,2) in a){ ##Checking condition if line starts from > and sub-string from 2nd character to till all line is present in array a then do following.
found=1 ##Setting found to 1 here.
val=$0 ##Creating val which has current line value here.
next ##next will skip all further statements from here.
}
found!=""{ ##Checking if found is SET then do following.
print val ORS $0 ##Printing variable val ORS and current Line here.
found="" ##Nullifying found here.
}' Input_file1 Input_file2 ##Mentioning Input_file names here.
I wanted to merge a string ABC to a line that I have extracted from a file.
The following command is used to extract the lines 20-25 in file_ABC, take only the first column, which is then transposed to become a row (or line).
sed -n '20,25p' < file_ABC | awk '{print $1}' | paste -s
This is the result:
2727778 14734 0 0 0 2713044
I would like to add at the first position of this line the string ABC.
ABC 2727778 14734 0 0 0 2713044
Any suggestion on how to do that?
A quick hack would be to use something like
printf 'ABC\t%s\n' "$(sed -n '20,25p' < file_ABC | awk '{print $1}' | paste -s)"
You could modify your initial command instead to use awk for everything, though:
awk '
BEGIN {printf "ABC"}
NR>=20 && NR<=25 {printf "\t%s", $1}
END {print ""}
' file_ABC
This might work for you (GNU sed):
sed '20,25{s/\s.*//;H};$!d;x;s/^/ABC/;s/\n/ /g' file
Gather up the first column fields by appending them to the hold space for rows 20 to 25 only. At the end of the file prepend ABC and replace the introduced newlines by spaces.
For fun, bash only
filename=file_ABC
words=("${filename##*_}")
i=0
while read -r word rest_of_line; do
((++i < 20 )) && continue
(( i > 25 )) && break
words+=("$word")
done < "$filename"
join() { local IFS=$1; shift; echo "$*"; }
join $'\t' "${words[#]}"
But this will be much slower than a single awk call.
if you want to keep all in one script
$ awk 'BEGIN {line="ABC"}
NR>=20 && NR<=25 {line=line FS $1}
NR==25 {print line; exit}' file
improved version as suggested by #EdMorton
$awk 'NR>=20 {line=line OFS $1}
NR==25 {print "ABC" line; exit}' file
I am trying to swap the first line with last line in a text file in unix
file has:
line1
line2
line3
line4
line5
line6
I want like this:
line6
line2
line3
line4
line5
line1
I am using sed -n -e '1 s/^.*$/$p' file . Which is not happening.
EDIT2: As per Ed sir's comment adding this solution too here which will be more suitable in case 2nd line is empty then also it will work.
awk 'NR==1{first=$0;next} NR>2{val=val prev ORS} {prev=$0} END{print prev ORS val first} Input_file
EDIT: To exchange only first and last line following may help you(considering that your Input_file is same as shown sample).
awk 'FNR==1{first=$0;next} {val=(val?val ORS prev:prev?$0:"")} {prev=$0} END{print $0 ORS val ORS first}' Input_file
Explanation:
awk '
FNR==1{ ##Checking if line is first line then do following.
first=$0; ##Creating varable first whose value is current line value.
next ##next will skip all further statements from here.
}
{
val=(val?val ORS prev:prev?$0:"") ##Creating variable named val here whoe value is concatenating to its own value with variable prev value.
}
{
prev=$0 ##Creating variable prev whose value will be current value of line but will become previous value for next line.
}
END{
print $0 ORS val ORS first ##Printing current line ORS val ORS and first here.
}' Input_file ##Mentioning Input_file name here.
Could you please try following and let me know if this helps you.
awk -v from=1 -v to=6 'FNR==from{source=$0;next} FNR==to{target=$0;next} {val=val?val ORS $0:$0} END{print target ORS val ORS source}' Input_file
In sed, you'll need to make two passes; it probably makes more sense to use ed instead. Then we just need two move commands - move the last line to just after line 1, then move the first line to the end:
$m1
1m$
Demo
#!/bin/bash
set -euo pipefail
# Create our input file
file=$(mktemp)
trap 'rm "$file"' EXIT
cat >"$file" <<END
line1
line2
line3
line4
line5
line6
END
echo Before:
cat "$file"
echo
# Make the change
ed -s "$file" <<<$'$m1\n1m$\nwq'
# Show the result
echo After:
cat "$file"
If you need to write a different output file, you can of course add a filename argument to the wq command, as usual.
This might work for you (GNU sed):
sed 'H;$!d;x;s/\n\([^\n]*\)\(\n.*\n\)\(.*\)/\3\2\1/' file
Copy the whole file into the hold space (HS) and after the last line, swap to the HS, split the file into first line, middle and last line and substitute the first and last lines.
A two pass alternative solution:
sed -n '1s/.*/$c&/p;$s/.*/1c&/p' file | sed -f - file
Create a sed script to change the first line to the last and last line to the first.
Another solution using cat, head, tail and sed:
cat <(tail -1 file) <(sed '1d;$d' file) <(head -1 file)
Another sed solution:
sed -E '1h;1d;:a;N;$!ba;s/(.*)\n(.*)/\2\n\1/;G' file
Just in case a oneliner mixing, head, tail and sed:
$ FIRST=$(head -1 file.txt) LAST=$(tail -1 file.txt) \
sed "1 s/^.*$/${LAST}/" file.txt | sed "$ s/^.*$/${FIRST}/"
For very large files, you might be interested in a double pass:
awk '(NR!=FNR){print (FNR==1 ? t : (FNR==c ? h : $0) ); next }
(NR==1){h=$0}{t=$0;c=NR}' file file
Using the sponge util:
f=file
{ tail -1 $f ; tail -n +2 $f | head -n -1 ; head -1 $f ; } | sponge $f
I have the below lines in a file
id=1234,name=abcd,age=76
id=4323,name=asdasd,age=43
except that the real file has many more tag=value fields on each line.
I want the final output to be like
id,name,age
1234,abcd,76
4323,asdasd,43
I want all values before (left of) the = to come out as separated with a , as the first row and all values after the (right side) of the = to come below for in each line
Is there a way to do it with awk or sed? Please let me know if for loop is required for the same?
I am working on Solaris 10; the local sed is not GNU sed (so there is no -r option, nor -E).
$ cat tst.awk
BEGIN { FS="[,=]"; OFS="," }
NR==1 {
for (i=1;i<NF;i+=2) {
printf "%s%s", $i, (i<(NF-1) ? OFS : ORS)
}
}
{
for (i=2;i<=NF;i+=2) {
printf "%s%s", $i, (i<NF ? OFS : ORS)
}
}
$ awk -f tst.awk file
id,name,age
1234,abcd,76
4323,asdasd,43
Assuming they don't really exist in your input, I removed the ...s etc. that were cluttering up your example before running the above. If that stuff really does exist in your input, clarify how you want the text "(n number of fields)" to be identified and removed (string match? position on line? something else?).
EDIT: since you like the brevity of the cat|head|sed; cat|sed approach posted in another answer, here's the equivalent in awk:
$ awk 'NR==1{h=$0;gsub(/=[^,]+/,"",h);print h} {gsub(/[^,]+=/,"")} 1' file
id,name,age
1234,abcd,76
4323,asdasd,43
FILE=yourfile.txt
# first line (header)
cat "$FILE" | head -n 1 | sed -r "s/=[^,]+//g"
# other lines (data)
cat "$FILE" | sed -r "s/[^,]+=//g"
sed -r '1 s/^/id,name,age\n/;s/id=|name=|age=//g' my_file
edit: or use
sed '1 s/^/id,name,age\n/;s/id=\|name=\|age=//g'
output
id,name,age
1234,abcd,76 ...(n number of fields)
4323,asdasd,43...
The following simply combines the best of the sed-based answers so far, showing you can have your cake and eat it too. If your sed does not support the -r option, chances are that -E will do the trick; all else failing, one can replace R+ by RR* where R is [^,]
sed -r '1s/=[^,]+//g; s/[^,]+=//g'
(That is, the portable incantation would be:
sed "1s/=[^,][^,]*//g; s/[^,][^,]*=//g"
)
I have a function
xyz()
{
x=$1*2
echo x
}
then I want to use it to replace a particular column in a csv file by awk.
File input.csv:
abc,2,something
def,3,something1
I want output like:
abc,4,somthing
def,6,something1
Command used:
cat input.csv|awk -F, -v v="'"`xyz "$2""'" 'BEGIN {FS=","; OFS=","} {$2=v1; print $0}'
Open file input.csv, calling function xyz by passing file 2nd filed as argument and result is stored back to position 2 of file, but is not working!
If I put constant in place of $2 while calling function it works:
Please help me to do this.
cat input.csv|awk -F, -v v="'"`xyz "14""'" 'BEGIN {FS=","; OFS=","} {$2=v1; print $0}'
This above line of code is working properly by calling the xyz function and putting the result back to 2nd column of file input.csv, but with only 14*2, as 14 is taken as constant.
There's a back-quote missing from your command line, and a UUOC (Useless Use of Cat), and a mismatch between variable v on the command line and v1 in the awk program:
cat input.csv|awk -F, -v v="'"`xyz "$2""'" 'BEGIN {FS=","; OFS=","} {$2=v1; print $0}'
^ Here ^ Here ^ Here
That should be written using $(…) instead:
awk -F, -v v="'$(xyz "$2")'" 'BEGIN {FS=","; OFS=","} {$2=v; print $0}' input.csv
This leaves you with a problem, though; the function xyz is invoked once by the shell before you start your awk script running, and is never invoked by awk. You simply can't do it that way. However, you can define your function in awk (and on the fly):
awk -F, 'BEGIN { FS = ","; OFS = "," }
function xyz(a) { return a * 2 }
{ $2 = xyz($2); print $0 }' \
input.csv
For your two-line input file, it produces your desired output.