|
|
|
sed dropping last line of file
|
Original Message
|
Name: gismapper
Date: June 6, 2003 at 12:04:23 Pacific
Subject: sed dropping last line of fileOS: SunOS 5.8CPU/Ram: not sure |
Comment: Greetings... I would be very thankful for any insight into this situation: It seems to me as though a text file I've received does not have a new line character at the end of it. The file is called 149.dat, and it's supposed to contain text formatted as follows: 19492pa091 19493pa029 If I do a tail on the file (tail 149.dat) I get this: 19492pa091 19493pa029 19494pa029 19495pa029 19496pa029myusername@myhostname If I type: sed -n '$p' 149.dat I get nothing back. I would like to add commas with sed as follows: cat 149.dat|sed -e "s/...../&,/" -e "s/...$/,&/" The output would then look like this: 19492,pa,091 19493,pa,029 19494,pa,029 19495,pa,029 19496,pa,029 19496,pa,029 It works for every line in the file except the last line, which disappears. Any ideas? This is happening in a script in ksh. Thanks for your time!
Report Offensive Message For Removal
|
|
Response Number 1
|
Name: nails
Date: June 6, 2003 at 13:26:37 Pacific
|
Reply: (edit)Hi: I think you're right about missing a line-feed at the end of the last line. Under ksh, you could append one: printf '\012' >> data.file where \012 is the octal value for a LF. Regards, Nails
Report Offensive Follow Up For Removal
|
|
Response Number 2
|
Name: gismapper
Date: June 6, 2003 at 20:45:00 Pacific
|
Reply: (edit)Thanks, Nails! Works great to append the line feed onto the source file. If I could ask a follow-up question: I'd like to eliminate as many intermediate files as possible. The input data file is actually the result of an unzip command. I noticed that unzip has a -p argument that lets you pipe the output to stdout. I'm guessing the sequence would be something like: unzip -p 194.zip|sed -e "s/...../&,/" -e "s/...$/,&/" If I wanted to avoid an intermediate file between the unzip and the sed commands, how would I slap the printf '\012' in between the unzip and the sed commands? If I executed the above line, I would lose the last line of the zip file, since it needs the extra line feed. If I just pipe the unzip output to printf, how would I tell printf to output the stuff that's being piped in, as well as the new line feed? Thanks again for all the help! GISmapper
Report Offensive Follow Up For Removal
|
|
Response Number 3
|
Name: nails
Date: June 7, 2003 at 19:50:51 Pacific
|
Reply: (edit)GISmapper: I still learning sed, but this might work: sed '$a\ '"$(printf '\012')"' ' pipe your unzip command to the stub sed command above. The $a\ is sed's append to the end of file syntax. I'm appending a line feed. Yes, it must be 3 lines and the quotes the way I present them is important. You should then be able to pipe the output of the stub to the rest of your script. Also, maybe you can make it part of sed insert comma stuff. Regards, Nails
Report Offensive Follow Up For Removal
|
|
Response Number 4
|
Name: GISmapper
Date: June 9, 2003 at 07:29:00 Pacific
|
Reply: (edit)Nails: I tried the code you recommended, but got the same results. I may be wrong, but it looks as though sed will always drop the last line if it doesn't find a newline at the end of it. Any ideas on how to pipe the unzip data plus a newline to sed, without using sed? Thanks, GISmapper
Report Offensive Follow Up For Removal
|
|
Response Number 5
|
Name: nails
Date: June 9, 2003 at 08:23:56 Pacific
|
Reply: (edit)GISmapper: I was afraid of this. Obviously, sed's not recognizing you're last line because it's not LF terminated. I'll be the other unix commands are the same. If I think of anything else, I'll let you know, but I have no other ideas. Regards, Nails
Report Offensive Follow Up For Removal
|
|
Response Number 6
|
|
Reply: (edit)Per the sed manual: "Only complete input lines are processed. Any input text at the end of a file that is not terminated by a new-line character is ignored." But I think most unix commands will process the line (cat, tail, awk ..) Instead of piping into sed, you could pipe into awk: unzip -p 194.zip | awk '{print substr($0,1,5) "," substr($0,6,2) "," substr($0,8,3) }'
Report Offensive Follow Up For Removal
|
|
Response Number 7
|
Name: GISmapper
Date: June 9, 2003 at 10:27:49 Pacific
|
Reply: (edit)Nails, James: Thanks for your help, guys. The awk solution is the faster of the two, so I'll be using it, but I'm very grateful to both of you and to everyone else who regularly contributes to this forum. It's an huge help to those of us just starting out. GISmapper
Report Offensive Follow Up For Removal
|
Use following form to reply to current message:
|
|

|