Multiple lists of Books with ISBN,TITLE,OBSERVATION and other with OBSERVATION,TITLE,ISBN, I want remove column OBSERVATION, print TITLE as 1st column and ISBN as 2nd as follow.
Input:
- 123654 Cosmology Updated
- 35647 Medecine Revised
- 987456 Juriprudence Revised
- Updated Rhetorics 123456
- Revised epistomolgy 654321
Ouput desired:
- Cosmolgy 123654
- Medecine 35647
- Rhetorics 123456
- epistomolgy 654321
I tried this code found in SE and manipulated for hours without result
awk -v OFS='t' 'NR==1{for (i=1;i<=NF;i++)if ($i=="Updated"){n=i-1;m=NF-(i==NF)}} {for(i=1;i<=NF;i+=1+(i==n))printf "%s%s",$i,i==m?ORS:OFS}' file
Advertisement
Answer
EDIT: Since OP confirmed in comments that Input_file is NOT comma separated then by seeing OP’s attempt, could you please try following then.
awk '{if($1~/^[0-9]+/){print $2,$1} else {print $2,$3}}' Input_file
Explanation: Adding explanation for above code.
awk ' ##Starting awk program from here. { if($1~/^[0-9]+/){ ##Checking if condition if a line starts with digit then do following. print $2,$1 ##Printing 2nd field and then 1st field of line here. } else{ ##Using else in case that above if condition is NOT TRUE then do following. print $2,$3 ##Printing 2nd and 3rd field of line. } } ' Input_file ##Mentioning Input_file name here, which awk is processing.
Following solutions considered that OP’s Input_file is csv and comma separated.
Looks like your Input_file is comma separated and you want to print 1st field of space separated 2nd column and 1st field of line then. If this is the case could you please try following.
awk ' BEGIN{ FS=OFS="," } { split($2,array," ") print array[1] $1 } ' Input_file
Explanation: Adding detailed explanation for above code.
awk ' ##Starting awk program from here. BEGIN{ ##Starting BEGIN section from here. FS=OFS="," ##Setting comma for FS and OFS here for all lines of Input_file. } { ##Starting main BLOCK from here for this program. split($2,array," ") ##Splitting 2nd field to array named array whose delimiter is space. print array[1] $1 ##Printing array 1st element and 1st field of current line. } ' Input_file ##Mentioning Input_file name here.
In case your complete Input_file is comma separated then you need not only print fields like following.
awk ' BEGIN{ FS=OFS="," } { print $2,$1 } ' Input_file
Explanation: Adding explanation for above code.
awk ' ##Starting awk program from here. BEGIN{ ##Starting BEGIN section from here. FS=OFS="," ##Setting comma for FS and OFS here for all lines of Input_file. } { print $2,$1 ##Printing each line 2nd field and 1st field. } ' Input_file ##Mentioning Input_file name here.