5

I have a .csv file with contents similar to this:

BIHAR,PURNIA,DAGARUA,BELGACHHI,BELGACHHI,KARBOLA TOLA,0,0,312,0,0,312,Fully Covered,NO,NO,01_04_2010,241656,312,2123,910,1811.5
BIHAR,PURNIA,SRINAGAR,THARI,THARI,ARBANNA,0,0,312,0,0,312,Fully Covered,NO,NO,01_04_2010,244374,312,2123,910,1811.5
BIHAR,PURNIA,RUPAULI,DHOBGIDHA-RUPAULI,DHOBHGIDHA-RUPAULI-II,MATELI,0,0,312,0,0,312,Fully Covered,NO,NO,01_04_2010,243748,312,2123,910,1811.5
ETCETC,PURNIA,KRITYANAND NAGAR,CHUNAPUR,BANBHAG,BANGALI TOLA KOSHI KINARA,0,0,312,0,0,312,Fully Covered,NO,NO,01_04_2010,242663,312,2123,910,1811.5

I want to grab all the lines that start with BIHAR and then output it to another separate csv file. How do I do that?

I have tried using sublime's "Find All" feature and then use the right arrow to the end of the line to highlight them, but unfortunately some lines are much longer than the others so it doesn't work. There are about 100'000 lines in the .txt file.

I also tried with sed:

sed -n 'BIHAR /myfile.txt' /newfile.txt

EDIT: For some reason grep/sed/awk ignores the newlines at the end of each line, and so as a result it only attempts to match the first line and nothing else, how do I fix this?

9

Try this with GNU sed:

sed -n '/^BIHAR/p' file > new_file

or with grep:

grep '^BIHAR' file > new_file

or with awk:

awk '/^BIHAR/' file > new_file
  • Hi, sorry I mistyped it's actually a .csv file not a .txt file. As a result the new_file is empty. I have tried your suggestions with a plain text file and it works well! Would you happen to know what to do with a csv? – ifma Oct 25 '15 at 16:29
  • Well actually I found the issue, sed/grep/awk doesn't take into account the newline at the end of each line, so it only gets the first thing in the above .csv file and nothing else. – ifma Oct 25 '15 at 16:56
  • First remove carriage returns from Windows in your file: tr -d '\r' < orignal_file > converted_file or use something like this: tr -d '\r' < file | grep '^BIHAR' > new_file – Cyrus Oct 25 '15 at 17:01
  • Hmm it still returns an empty file :(. I tried first removing the carriage return as in the first part of your comment, then doing sed, but what that did is put everything into one giant line in the new file – ifma Oct 25 '15 at 17:13
  • Upload your csv file somewhere where I can download it to inspect your file. – Cyrus Oct 25 '15 at 17:17
1

grep's

 grep ^BIHAR file > new_file

awk's

 awk '/^BIHAR/' file > new_file

pedantic awk's

 awk -F, '$1 == "BIHAR" { print $0 ;} file > new_file

edit: when using file comming from windows world, be sure to use dos2unix to convert end-of-line

  dos2unix dosfile > unixfile

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.