Any shell scripts for cutting and pasting part of data?

Hi,

I have a tab-delimited txt file as below. It is part of the original file.

I want to cut the lines starting with "3" in column1 and paste them before the lines starting with "1" in column 1. So I will get

Anyone knows any simple shell scripts to do that? The original file is too big so I want to just use shell scripts to process that data.

Thanks

-C

A lame way of doing it:

sed '/^3/d' <yourdata.txt >temp1
sed '/^3/!d' <yourdata.txt >temp2
cat temp2 temp1 >result

This answer is based on your original posting exact specifications.

Lines before line starting with �1�:

sed -n '/^1/q;p' inp_file > partI

Lines between line starting with '1' (inclusive) and starting with '3' (exclusive):

sed -n '/^3/q;/^1/,/^3/p' inp_file > partIII

Lines starting with '3':

egrep '^3' inp_file > partII

Put together:

cat partI partII partIII

Thanks very much for both of you!

one line awk:

awk -F'\t' 'BEGIN{i=1;}NR==FNR{if ($1~"^3") a[i\+\+]=$0} NR>FNR{if (FNR<=3){print $0;next;} if ($1!~"^3")a[i\+\+]=$0} END{for(j=1;j<i;j++) print a[j]; }' input input

output:

##Hello
##Welcome
#C1	C2	C3
3	3	3
3	3	3
1	1	1
2	2	2

@sk1418, using line number is bad row with "1" in column 1 might not always be on row 3. Something like this would be better:

awk -F'\t' 'NR==FNR{if($1=="3")a[r++]=$0;next} r&&$1=="1"{for(i=0;i<r;i++)print a;r=0} $1!="3"' input input