sed skipping the last line in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed skipping the last line in a file
# 1  
Old 03-19-2008
sed skipping the last line in a file

Hello All,
I have a file like this:

PART A1
PART A2
%
PART B1
PART B2

The last line in the file (PART B2) does not end with a new line. i.e <EOF> immediately follows "PART B2".

Here is my problem: I want to print everything after '%'.

So, I tried:
sed '1,/%/ d' [file_name].

This works beautifully on Linux. But on Solaris, the last line is never printed. I suspect since it does not end with a newline, sed quits abruptly after encountering <EOF>.

Any ideas on how to get around this problem?

Thanks,

Ali
# 2  
Old 03-19-2008
What happens if you try the following on Solaris?

sed -n '/%/,$p' file | sed '1d'
# 3  
Old 03-19-2008
Same thing as before. sed -n '/%/,$ p' returns:
%
PART B1


sed -n '/%/,$ p' | sed '1d' returns:

PART B1
# 4  
Old 03-19-2008
Please provide output from od -c file
# 5  
Old 03-19-2008
Quote:
Originally Posted by aghoras
Hello All,
I have a file like this:

PART A1
PART A2
%
PART B1
PART B2

The last line in the file (PART B2) does not end with a new line. i.e <EOF> immediately follows "PART B2".

Here is my problem: I want to print everything after '%'.

So, I tried:
sed '1,/%/ d' [file_name].

This works beautifully on Linux. But on Solaris, the last line is never printed. I suspect since it does not end with a newline, sed quits abruptly after encountering <EOF>.

If the last line does not end with a newline, it does not meet the POSIX definition of a text file, which is what sed is designed to deal with.
Quote:
Any ideas on how to get around this problem?

Add a newline to the end of the file.
# 6  
Old 03-20-2008
od -c says:
0000000 P A R T A 1 \r \n P A R T A 2
0000020 \r \n % \r \n P A R T B 1 \r \n P A
0000040 R T B 2
0000045
# 7  
Old 03-20-2008
Quote:
Originally Posted by aghoras
od -c says:
0000000 P A R T A 1 \r \n P A R T A 2
0000020 \r \n % \r \n P A R T B 1 \r \n P A
0000040 R T B 2
0000045
The CR+LF (\r\n) combination suggests that this is a dos file. Run dos2unix on this file before parsing it with sed.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed command to replace a line in a file using line number from the output of a pipe.

Sed command to replace a line in a file using line number from the output of a pipe. Is it possible to replace a whole line piped from someother command into a file at paritcular line... here is some basic execution flow.. the line number is 412 lineNo=412 Now i have a line... (1 Reply)
Discussion started by: vivek d r
1 Replies

2. Shell Programming and Scripting

sed to replace a line with modified line in same file

i have few lines in a file... i am reading them in a while loop so a particular line is held is $line1.. consider a modified line is held in $line2.... i want to replace $line1 with $line2 in the same file... how to do it..? i have come up till the below code sed "s/$line1/$line2/g" tmpfile.sql... (5 Replies)
Discussion started by: vivek d r
5 Replies

3. UNIX for Dummies Questions & Answers

Appending 2 files skipping the header of the second file

I have 2 files with the same header and need to append them and put the result in a 3rd file the 2 files has the same header and while appending i want to skip the second file header and need the result to be put in a third file Normally, this would work Cat file1 file2 >> file3....But how... (5 Replies)
Discussion started by: saggiboy10
5 Replies

4. Shell Programming and Scripting

sed on a one line file

Hello, First please excuse me for my poor english, i am french. Thank you for reading this thread and potentially helping me. I have a 300ko file of one line (without any \n) I want to create a new flat file with \n every 592 characters I tried with some sed commands, but i am not very... (10 Replies)
Discussion started by: marcello_fr
10 Replies

5. Shell Programming and Scripting

Extract a number from a line in a file and sed in another copied file

Dear all, I am trying to extract a number from a line in one file (task 1), duplicate another file (task 2) and replace all instances of the strings 300, in duplicated with the extracted number (task 3). Here is what I have tried so far: for ((k=1;k<4;k++)); do temp=`sed -n "${k}p"... (2 Replies)
Discussion started by: mnaqvi
2 Replies

6. Shell Programming and Scripting

how to use sed for reading from a file, line by line?

I have a file, from where I need to extract some data. But, the condition is like following : The script will read line by line,while checking if any line starts with 'MN'. If found true, it looks for if the immediate line starts with 'PQ' or not. If its true then, extract few fields from that... (1 Reply)
Discussion started by: mady135
1 Replies

7. Shell Programming and Scripting

sed to read line by line and input into another file

I have two files. Fileone contains text string one text string two text string three Filetwo contains Name: Address: Summary: Name: Address: Summary: Name: Address: Summary: I would like to use sed to read each line of file one and put it at the end of the summary line of file... (3 Replies)
Discussion started by: dolacap
3 Replies

8. Shell Programming and Scripting

sed -e remove line from file

Hi Trying to remove line from file log_January_1_array and code below doesn't work. $(sed -e '/"$n"/d' <log_January_1_array >log_January_1_array_1) sed doesn't know what is in $n variable and nth happens. Please advice how to make sed running this. thx (2 Replies)
Discussion started by: presul
2 Replies

9. Shell Programming and Scripting

sed delete pattern skipping first n lines of file.

I have files of more than 10K lines that I need to delete lines that contain a pattern, but I want to keep the first few lines intact. Can this be done with sed? (7 Replies)
Discussion started by: tkg
7 Replies

10. Shell Programming and Scripting

Parsing a file using perl and skipping some lines

Hi, Consider following file with input: `YFLG:NC^Byad_insert constraint {id=600104470} {profile=GENDER == 2} {profile=BEHAVIOR == 17} {profile=SITEATTR_MULT == siteid:211051} {profile=AGE in } yad_insert ad {id=1718286093336959379} {type=R} ^AYFLG:YOO^Byad_insert constraint {id=600104471}... (1 Reply)
Discussion started by: bvids
1 Replies
Login or Register to Ask a Question