Sponsored Content
Full Discussion: Edit a file using awk ?
Top Forums Shell Programming and Scripting Edit a file using awk ? Post 302595282 by jimbob01 on Thursday 2nd of February 2012 02:26:00 PM
Old 02-02-2012
Edit a file using awk ?

Hey guys,

I'm trying to learn a bit of awk/sed and I'm using different sites to learn it from, and i think I'm starting to get confused (doesn't take much!).

Anyway, say I have a csv file which has something along the lines of the following in it:
Code:
"test","127.0.0.1","startup timestamp",,,,"1327702381482",
"test","127.0.0.1","cpu combined","cpu 0",,,,"0.0900"
"test","127.0.0.1","cpu idle","cpu 0",,,,"0.9100"
"test","127.0.0.1","cpu nice","cpu 0",,,,"0.0000"
"test","127.0.0.1","cpu sys","cpu 0",,,,"0.0360"
"test","127.0.0.1","cpu user","cpu 0",,,,"0.0540"
"test","127.0.0.1","cpu wait","cpu 0",,,,"0.0010"

Basically, what I want, is to edit the file via a script rather than manually. For the first line for instance, all I want left on that line is startup timestamp,1327702381482 and on the second line cpu combined 0.0900 etc etc so that the file now looks something like:
Code:
startup timestamp,1327702381482
cpu combined,0.0900
cpu idle,0.9100
cpu nice,0.0000
cpu sys,0.0360
cpu user,0.0540
cpu wait,0.0010

Anyway, while learning, I've tried various different commands to do this, so, say for the first line, I tried the following (and it didn't work!):
Code:
 awk '{if (NR==1) print{"$19"} print{"$24"} print{"$26"}}' myfile.csv > mynewfile.csv

Something tells me I'm hopeless at this! Any help would be gratefully received before it drives me insane!

Also, I read that the O'Reilly sed&awk (second edition) book is worth buying, any you guys recommend it ? I looked it up on Amazon and it was published in 1997 but seems the current edition. Or, if you guys could recommend another book on sed/awk, there would be much thanks!

Cheers

Jim
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

edit entire column from a fixed-width file using awk or sed

Col1 Col2 Col3 Col4 12 Completed 08 0830 12 In Progress 09 0829 11 For F U 07 0828 Considering the file above, how could i replace the third column the most efficient way? The actual file size is almost 1G. I am... (10 Replies)
Discussion started by: tamahomekarasu
10 Replies

2. Shell Programming and Scripting

File edit with awk or sed

I have the follwoing file: This looks to be : seperated. For the first field i want only the file name without ".txt" and also i want to remove "+" sign if the second field starts with "+" sign. Input file: Output file: Appreciate your help (9 Replies)
Discussion started by: pinnacle
9 Replies

3. Shell Programming and Scripting

Sed or Awk or both to edit file

What is an efficient way to remove all lines from the input file which contain a file name? inputfile: ======================= # comment # comment # comment 5 8 10 /tmp 5 8 10 /var/run 5 8 10 /etc/vfstab 5 8 9 /var/tmp 5 8 10 /var/adm/messages... (7 Replies)
Discussion started by: Arsenalman
7 Replies

4. Shell Programming and Scripting

search and edit in the same file using awk

Hi, I am having a user.txt contains the name of users and passwd.txt file contains as passwd.txt $cat usr.txt root bin daemon cap $cat passwd.txt root:x:0:0:root:/root:/usr/bin/ksh bin:x:1:1:bin:/bin:/sbin/csh daemon:x:2:2:daemon:/sbin:/usr/bin/ksh adm:x:3:4:adm:/var/adm:/sbin/nologin... (4 Replies)
Discussion started by: Manabhanjan
4 Replies

5. Shell Programming and Scripting

edit fields awk

Hi there, i need some help please... I have this text, it's name data.txt that contains the following information: Mark Owen: 6999999888 6999999888 +302310999999 2310999999 Steve Blade Pit: +30691111222 2310888777 6999999888 John Rose: 2310777555 310544565 +302310999999 Mary Stuart:... (7 Replies)
Discussion started by: Mark_orig
7 Replies

6. Shell Programming and Scripting

use awk to edit a file..pls help

hey i want to over write the fourth field of a ':' delimited file by first finding the required row by using grep. i have done the following cat file | grep no. | awk -F ':' { $4=count; print $1:$2:$3:$4;} the correct values are being printed but nothin is bein added to the file..please... (5 Replies)
Discussion started by: dhe.arora
5 Replies

7. Shell Programming and Scripting

How to get awk to edit in place and join all lines in text file

Hi, I lack the utter fundamentals on how to craft an awk script. I have hundreds of text files that were mangled by .doc format so all the lines are broken up so I need to join all of the lines of text into a single line. Normally I use vim command "ggVGJ" to join all lines but with so many... (3 Replies)
Discussion started by: n00ti
3 Replies

8. Shell Programming and Scripting

edit field using sed or awk

please help me to edit the second field using awk or sed i have input file below aa1001 000001 bb1002 000002 cc1003 000003 so i want the output file like below aa1001 01 bb1002 02 cc1003 03 (38 Replies)
Discussion started by: zulabc
38 Replies

9. Shell Programming and Scripting

Inline edit using sed / awk

Hi, I have file with all the lines as following format <namebindings:StringNameSpaceBinding xmi:id="StringNameSpaceBinding" name="ENV_CONFIG_PATH" nameInNameSpace="COMP/HOD/MYSTR/BACKOFFICE/ENV_CONFIG_PATH" stringToBind="test"/> I want to replace (all the lines) value of... (8 Replies)
Discussion started by: shuklaa02
8 Replies

10. Shell Programming and Scripting

Edit distance using perl or awk

Dear all, I am working on a large Sindhi lexicon which I hope to complete by 2017 and place in open source. The database is in Arabic script in two columns delimited by an equal to sign. Column 1 contains a word or words without the short vowel and also some extraneous information which is... (0 Replies)
Discussion started by: gimley
0 Replies
All times are GMT -4. The time now is 12:38 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy