How to filter

Hi I have a file containing the below lines

1010001001639
1010001001789
1020001001927
1030001001928
1040001002033
1200001002609
1200001003481
1200001004935

I need to filter lines that starts with 101.

It would be of great help if its in awk.

awk '/^101/' file

Thanks Bartus... Very kind of u:p

---------- Post updated at 12:41 PM ---------- Previous update was at 12:34 PM ----------

But the below is not working

for cc in 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 139
do
cat temp | awk '/^$cc/' >acc_$cc
done

use this

awk '/^'$cc'/' temp >acc_$cc

instead of this

cat temp | awk '/^$cc/' >acc_$cc
1 Like

Or..

STR=101
grep "^$STR" temp > acc_${STR}

Use double quote instead of single:

for cc in 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 139
do
cat temp | awk "/^$cc/" >acc_$cc
done

Instead of it, you can try:

 
awk '{ s=substr($0,1,3); print >"acc_"s;}'  temp
1 Like