How to grep after the first comma till the next comma in a line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to grep after the first comma till the next comma in a line
# 1  
Old 05-09-2012
How to grep after the first comma till the next comma in a line

Hi

Can any one pls tell me how to grep this line

POPULATION,69691,20120509

I want the number 69691 from the above line.
How to grep from the first comma till the next comma.

Thank You.Smilie
# 2  
Old 05-09-2012
grep doesn't understand columns, but awk does.

Code:
awk -F, '{ print $2 }' filename

Other tools you could use include cut.
# 3  
Old 05-09-2012
Using regex:

Code:
sed "s/[^,]*,\([^,]*\),.*/\1/" filename

# 4  
Old 05-09-2012
Question

Hi Sir

They are 4 lines in the file.

One line is what i mentioned earlier


POPULATION,69691,20120509

From each line i just need to get the second value,
I studied the awk command and understood what you mean,
But i need to do it for all 4 lines in that file and each line based on the first word..ie., POPULATION and need 69691 from that line for further scripting.

How do I use awk command and see whether the second value is not 0.
here 69691 is not 0..then i proceed to do somemore scripting.

Thank You...I learnt a new command awk after your post.
Can you pls, help me with writing it for the above line and matching whether the second line is not 0.
# 5  
Old 05-09-2012
Can you post the entire file?
# 6  
Old 05-09-2012
Power

Sir


basically this is an Audit file, which lets me know what each file contains
first column is the file name"POPULATION", second column is number of records (i shoudl see whether it is >0)(ie., means the file contains data or not) and the third column is date when the file was produced.(which i dont need)

Based on the Audit file results..i take one more step further.
POPULATION,69831,20120427
CITIZENS,78692,20120427
PERMANENT,0,20120427
COLLEGEKIDS,151891,20120427

The file contains the above 4 lines..i have to get the second column for
each line and if it is 0, stop the processing for that line.

POPULATION is > 0
and
PERMANENT IS 0....> SO NO PROCESSING FOR PERMANENT.

Thank You.
# 7  
Old 05-10-2012
Code:
#! /bin/bash

while read line 
do
    if [ $(echo $line | cut -d, -f2) -gt 0 ]
    then
        <more_processing>
    fi
done < audit_file.txt

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to extract fields from a CSV i.e comma separated where some of the fields having comma as value?

can anyone help me!!!! How to I parse the CSV file file name : abc.csv (csv file) The above file containing data like abv,sfs,,hju,',',jkk wff,fst,,rgr,',',rgr ere,edf,erg,',',rgr,rgr I have a requirement like i have to extract different field and assign them into different... (4 Replies)
Discussion started by: J.Jena
4 Replies

2. Shell Programming and Scripting

awk to parse comma separated field and removing comma in between number and double quotes

Hi Experts, Please support I have below data in file in comma seperated, but 4th column is containing comma in between numbers, bcz of which when i tried to parse the file the column 6th value(5049641141) is being removed from the file and value(222.82) in column 5 becoming value of column6. ... (3 Replies)
Discussion started by: as7951
3 Replies

3. Shell Programming and Scripting

How to remove comma from first and last line?

Hi Gurus, I need to remove comma from first and last line. I tried below code, but no luck. It only remove first line. Gurus, please help. awk -F"," '{if(NR==1||NR==$NR) print $1; else print $0}' TEST sampe file: ABC HEADER TOTAL RECORDS ARE 2.00,,,,... (13 Replies)
Discussion started by: ken6503
13 Replies

4. Shell Programming and Scripting

Replace spaces with underscores up to first comma but not after the comma

I have a comma delimited file of major codes and descriptions. I want to replace all occurrences of spaces with underscores up to the first comma (only in the first field), but not replace spaces following the comma. For instance I have the following snippet of the file: EK ED,Elementary and... (7 Replies)
Discussion started by: tdouty
7 Replies

5. Shell Programming and Scripting

Replace comma and blank with comma and number

I, I have a file and i need to replace comma and blank space with comma and 0. cat file.txt a,5 b,1 c, d, e,4 I need the output as cat file.txt a,5 b,1 c,0 d,0 (4 Replies)
Discussion started by: jaituteja
4 Replies

6. Shell Programming and Scripting

Need Help - comma inside double quote in comma separated csv,

Hello there, I have a comma separated csv , and all the text field is wrapped by double quote. Issue is some text field contain comma as well inside double quote. so it is difficult to process. Input in the csv file is , 1,234,"abc,12,gh","GH234TY",34 I need output like below,... (8 Replies)
Discussion started by: Uttam Maji
8 Replies

7. Shell Programming and Scripting

How to redirect in comma separated csv from grep

Hi, I am newbie in unix. Could someone tell me how do I redirect my grep output to a csv/excel ? I have used below command but the outputs are appearing in one column Not in different column. grep -e cmd -e cmd1 test.txt | cut -f 5 | sort | uniq -c> op.csv I do not understand how do I... (14 Replies)
Discussion started by: kmajumder
14 Replies

8. Shell Programming and Scripting

Listing all digits behind the comma with grep

Hello, Unix-Forums! 1.23456789 This an example number. It can be any number. I want grep to only find the digits behind the "." That means 23456789 should be the output in this case. How would I do that? (2 Replies)
Discussion started by: intelinside
2 Replies

9. Shell Programming and Scripting

Pull Data After Comma if 2 word before comma

Hi, I am trying to truncate word after comma in a file ONLY if there are already 2 words BEFORE comma. If there is one word or 3 or more words BEFORE comma, then I have to leave the data AS IS. See below for example. Input File : John Smith, Manager Smith, John Frank J F K... (2 Replies)
Discussion started by: msalam65
2 Replies

10. UNIX for Dummies Questions & Answers

Second comma to new line.

Hi, Input is comma delimited. How to convert every second comma ',' to a new line character. I know tr ',' '\n' <filename will convert every occarance. Input : 1,5,6,4,9,..n Output: 1,5 6,4 9,.. Thanks in advance. (3 Replies)
Discussion started by: deepakwins
3 Replies
Login or Register to Ask a Question