ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I need to be able to remove the ,073 and num='' from a logfile that contains many lines like below and place a comma in between. The values are different on each line though.
Which command did you use to remove the unwanted parts? Maybe just a slight modification can do the rest of the work. And what are the differences between the lines? Is the format exactly the same?
assuming, that all lines have the format "yyyy-mm-dd hh:mm:ss,fff num='nnn...'" you could use sed:
Code:
sed -r "s/([^ ]+)\s+([^,]+),[0-9]+\s+num='([0-9]+)'/\1, \2, \3/" logfile
There are many ways to define the matching patterns. I choosed one, which describes the line as exactly as needed, if all lines look similar. If the file contains lines, which look different but match the pattern abocve too, you might need to refine it.
Since the lines have a fixed format/length you can modify the awk command as follows, using the substr function to get part of the second and third field:
this assumes you want a TAB after the commas, as in your example. If you just need a blank space, substitute \t with a space in the command above.
In addition, the sed command suggested by jan61 is excellent. To actually modify the file you have to add the -i option (edit the file in place). A general advice: first test the sed command without the -i option to see and check the result on the standard output, then edit it with -i. Or do a backup copy of the original file.
...cat somelog.2009-01-05 | grep -v name-ID | grep somestring | grep num | awk '{print $1"\t"$2"\t"$3}' > somelog.2009-01-05-stg1
...
jan61, thanks for your line of sed code, however, might be something I'm doing wrong...
You didn't do something wrong, but your file format is different from the one I pasted from your previous post. You use a TAB as field delimiter, my sed uses only blanks in one pattern. That's why the pattern does not match and the lines are left unchanged. The fix is simple:
Code:
sed -r "s/([^\s]+)\s+([^,]+),[0-9]+\s+num='([0-9]+)'/\1, \2, \3/" somelog.2009-01-05-stg1
The \s matches blanks and tabs - so it should work with your file.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.