Find c28 to next comma in line of log file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find c28 to next comma in line of log file
# 1  
Old 02-19-2010
Find c28 to next comma in line of log file

I have somelogfile.log where I'm trying to find only the numbers which would start at c28 on the third line from the bottom and end at the next comma, so for somelogfile.log:
Code:
bunch of text - 0, some other text - 123
or
bunch of text - 123, some other text - 192831923

where I want either the number 0 or 123. I've gotten this far
Code:
tail -n 3 somelogfile.log | head -n 1 | cut -c28-30

which works, but I don't know how to make cut only read until it hits a comma, my code just works to give me 2 digits. Then I tried
Code:
b=`tail -n 3 somelogfile.log | head -n 1 | cut -c28-32`
echo ${b##0*/,}

which doesn't work

Last edited by unclecameron; 02-19-2010 at 02:02 PM..
# 2  
Old 02-19-2010
The point is you are not using delimiters but a postion with cut, and so the syntax will be -c charpos1-charpos2...
What you can do is effectively load the result in a new variable and work on it...
e.g. as an array and read from beginning until first non numeric...
# 3  
Old 02-19-2010
Does the text format before the desired column vary?

Can you show us some lines of the logfile?
# 4  
Old 02-19-2010
got it!
Code:
tail -n 3 somelogfile.log | head -n 1 | cut -c28-32 | sed 's/[^0-9]//g'

hope this helps someone else Smilie As always, let me know if there's a more elegant/better way to do it so I can improve my coding.

Franklin52: the text does vary, but it doesn't vary in character width
# 5  
Old 02-19-2010
With less commands:

Code:
tail -n 3 somelogfile.log | awk 'NR==1{print int(substr($0,28,5))}'

# 6  
Old 02-20-2010
yummy, awk is my new best friend! Dug into awk and tried to understand it a little better, very nice, thanks for the help, here's what I've come up with to get the same thing:

Code:
tail -n 4 somelogfile.log | awk 'NR==1{print int($4)}'

and this to get the last column (which I needed next):
Code:
tail -n 4 somelogfile.log | awk 'NR==1{print int($6)}'

Thanks for the help, I needed to learn awk better, but had been putting it off Smilie

Last edited by unclecameron; 02-20-2010 at 11:36 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

To find and display the middle line in a file using single line command.

Hi all, How can i display the middle line of a file using a single line command? (6 Replies)
Discussion started by: Lakme Pemmaiah
6 Replies

2. UNIX for Dummies Questions & Answers

Add a field separator (comma) inside a line of a CSV file

Hi... I can't find my little red AWK book and it's been a long while since I've awk'd. But I need to take a CSV file and convert the first word of the fifth field to its own field by replacing a space with a comma. This is for importing a spreadsheet of issues into JIRA... Example: a line... (9 Replies)
Discussion started by: Tawpie
9 Replies

3. UNIX for Advanced & Expert Users

How to find a string in a line in UNIX file and delete that line and previous 3 lines ?

Hi , i have a file with data as below.This is same file. But actual file contains to many rows. i want to search for a string "Field 039 00" and delete that line and previous 3 lines in that file.. Can some body suggested me how can i do using either sed or awk command ? Field 004... (7 Replies)
Discussion started by: vadlamudy
7 Replies

4. Shell Programming and Scripting

Adding comma to end of each line if more than 1 line

I have a file with dates as '2013-01-01' '2013-01-02' I want the output to be '2013-01-01','2013-01-02' if there is only 1 entry then there should not be any comma. (6 Replies)
Discussion started by: ATWC
6 Replies

5. Shell Programming and Scripting

How to Remove comma as last character in end of last line of file?

how to Remove comma as last charector in end of last line of file: example: input file --------------- aaaaaa, bbbbbb, cccc, 12345, ____________ output file : ----------- aaaaaa, bbbbbb, (6 Replies)
Discussion started by: RahulJoshi
6 Replies

6. UNIX for Dummies Questions & Answers

Remove First Char from Line in File Only if it's a comma

I have a file, I need to remove the first character of each line, but only if it's a comma. I don't want to delete any other commas in each line. Trying cat or sed but I really don't know them very well, would love some help. This removes the first comma, but it removes the first comma no... (6 Replies)
Discussion started by: Cynthia
6 Replies

7. Shell Programming and Scripting

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.:confused: (8 Replies)
Discussion started by: rxg
8 Replies

8. Shell Programming and Scripting

How to find duplicate line in log file?

Hi guys, I'm really happy to find this forum I have a log file, and I have to find all lines that have "error" word, and then save this output in file, the output file has to have just only one line to any Duplicated lines and counter that show how many time this lines duplicated? I already... (2 Replies)
Discussion started by: wax_light
2 Replies

9. Shell Programming and Scripting

find & replace comma in a .csv file.

HI, Please find the text below. I receive a .csv file on server. I need the comma(,) in the second column to be replaced by a semi-colon( ; ). How to do it. Please help. Sample text: "1","lastname1,firstname1","xxxxxx","19/10/2009","23/10/2009","0","N","Leave"... (2 Replies)
Discussion started by: libin4u2000
2 Replies

10. UNIX for Dummies Questions & Answers

How to remove comma from the last line of the file

Hi, I have a file which has records which end with a comma. for example: My file looks like 1234, 5678, 3455, 3566, 4444, 9999, I need to remove comma for the last line in the file so that my file should look like: 1234, 5678, 3455, (5 Replies)
Discussion started by: sandeep_1105
5 Replies
Login or Register to Ask a Question