|
|
|
awk help
|
Original Message
|
Name: Mehul
Date: September 18, 2002 at 14:12:10 Pacific
Subject: awk helpOS: sun solarisCPU/Ram: sun |
Comment: Hi, i am new to awk hope someone can help me i want to read first word from a file if that word is "yes" i want to copy that whole line in one file if the first word is "NO" i want to store that whole line in other file. The file contains many lines Thx Mel
Report Offensive Message For Removal
|
|
Response Number 1
|
Name: Jerry Lemieux
Date: September 18, 2002 at 16:25:54 Pacific
Subject: awk help
|
Reply: (edit)#!/bin/ksh Don't need awk for this task. Try this: #!/bin/ksh while read line do if [[ $line = "yes"* ]] then print $line >> yes.file elif [[ $line = "no"* ]] then print $line >> no.file fi done < yesNo.data Given a file that looks like this: yes 1 no 1 yes 2 yes 3 no 2 no 3 no 4 yes 4 yes 5 no 5 no 6 Your yes.file will look like this: yes 1 yes 2 yes 3 yes 4 yes 5 And your no.file will look like this: no 1 no 2 no 3 no 4 no 5 no 6 Korn shell scripting, it's fun for the whole family. Jerry
Report Offensive Follow Up For Removal
|
|
Response Number 2
|
Name: James Boothe
Date: September 18, 2002 at 17:01:47 Pacific
Subject: awk help |
Reply: (edit)Multiple runs of Jerry's script will keep appending to existing output files. If you want new output files each time, rather than appending to old files, his script should begin with: rm -f yes.file no.file 2> /dev/null In case you are wanting an awk solution in particular, that code follows. And while the coding is a little more concise, Jerry's ksh solution uses less cpu time. awk '\ $1=="yes" {print > "yes.file"} $1=="no" {print > "no.file"} ' yesNo.data
Report Offensive Follow Up For Removal
|
|
Response Number 3
|
Name: Jerry Lemieux
Date: September 18, 2002 at 20:11:20 Pacific
Subject: awk help
|
Reply: (edit)I normally do not use >>. I usually use exec 3>. Unfortunately, when you use left and right angle brackets in an html based discussion group like this, it is read as a tag and doesn't print properly. I will show and example here: exec 3 /tmp/junk.file.out When this is posted, the angle brackets will not be seen and the exec 4> /tmp/junk.file.out will not appear. Using exec 4> will recreate the file each time and using print -u4 will allow the line to be written to the output file. Jerry
Report Offensive Follow Up For Removal
|
|
Response Number 4
|
Name: James Boothe
Date: September 19, 2002 at 06:07:15 Pacific
Subject: awk help |
Reply: (edit)Thanks Jerry - good info. On the example exec command that you posted, was that one line or two lines? I am guessing that it was: exec 3> /tmp/junk.file.out exec 4> /tmp/junk.file.out in which case I don't think any of that will go away - at least it did not go away when I submitted this and looked at the preview message (in yellow). And we might be dealing with differences in browsers or browser settings. Also, since you are able to use the angle bracket expressions in your text discussion, then could you not use them in your code in the same manner? I never have any problems when posting single or double right-angle brackets (always space delimited), but if I post a left angle-bracket, that bracket and remainder of line (at least) go away, since I believe that signifies the beginning of a tag. To post a single left angle-bracket, I use the special 4-character string: ampersand-l-t-semicolon which works MOST of the time for me. In the following example, I used the above 4-character string twice in succession: cat << endcat just testing endcat
Report Offensive Follow Up For Removal
|
|
Response Number 5
|
Name: Jerry Lemieux
Date: September 19, 2002 at 08:34:59 Pacific
Subject: awk help
|
Reply: (edit)The exec 3< /tmp/junk.file.out line was left out of my example. The reason it disappears is that the response we create is being translated into html. When you use a left angle bracket and then later on use a right angle bracket, everything between the 2 angle brackets along with any text will disappear. Not really a function of the browser but rather html syntax. Makes it difficult to post examples where redirect is being used.
Report Offensive Follow Up For Removal
|
|
Response Number 6
|
Name: Jerry Lemieux
Date: September 19, 2002 at 08:42:41 Pacific
Subject: awk help
|
Reply: (edit)Here is one way it can be posted. Open the output file first and then the input. exec 3 > /tmp/output.file exec 4 < /tmp/input.file while read -u4 $line do print -u3 $line done If you have special characters in the input and output files, you would do a raw read which will keep \ and other special characters. An example would be: read -r -u4 print -r -u3 Jerry
Report Offensive Follow Up For Removal
|
|
Response Number 7
|
Name: James Boothe
Date: September 19, 2002 at 11:48:36 Pacific
Subject: awk help |
Reply: (edit)I like those file manipulation commands. But again, to post single or double angle brackets, you can use the special tags that I referred to in my previous reply so that you can post things like: >
Report Offensive Follow Up For Removal
|
|
Response Number 8
|
Name: James Boothe
Date: September 19, 2002 at 11:51:39 Pacific
Subject: awk help |
Reply: (edit)Oops, my apologies. The preview message (in yellow) showed proper, so I proceeded with confirming the post, and the data disappeared, as you can see. It would be nice if the preview message were an actual what-you-see-is-what-you-get preview. And another big issue is that it will not retain leading spaces, so there goes the nice indentation.
Report Offensive Follow Up For Removal
|
Use following form to reply to current message:
|
|

|