Problem with a text file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem with a text file
# 1  
Old 08-15-2014
Problem with a text file

Hi,

I have many text files with certain problem. It gets fixed by doing vi and delete first line (supposedly a blank line) by pressing dd and save the file.

I need to automate this process and I tried sed '1d' samp.pgn and/or unix2dos / dos2unix etc. but NONE of them worked. Strange thing about these text files is that vi shows 4 lines and wc -l shows 3 lines.

Please advise, many thanks!

Code:
$ wc -l samp.pgn
3 samp.pgn

Code:
$ od -c samp.pgn
0000000  \n       1   .   .   .       N   e   4   +       2   .       K
0000020   b   6       K   b   8       3   .       R   c   2       R   d
0000040   6       4   .       R   c   5       f   5       5   .       R
0000060   a   4       N   f   6       6   .       R   a   1       N   d
0000100   7   #       0   -   1          \n       1   .       Q   x   a
0000120   4       b   x   a   4       2   .       R   x   b   8       R
0000140   x   b   8       3   .       a   6       R   d   8       4   .
0000160       a   7       B   e   6       5   .       a   8   =   Q
0000200   R   x   a   8       6   .       B   x   a   8       B   x   a
0000220   2       7   .       B   c   6       f   5       8   .       B
0000240   x   a   4       K   f   7       9   .       R   a   1       B
0000260   d   5       1   0   .       B   b   5       g   6       1   1
0000300   .       B   c   4       K   e   6       1   2   .       R   a
0000320   7       B   g   5       1   3   .       R   x   h   7       B
0000340   x   d   2       1   4   .       B   x   d   5   +       K   x
0000360   d   5       1   5   .       R   d   7   +       K   e   4
0000400   1   6   .       R   x   d   2       c   4       1   7   .
0000420   R   d   6       1   -   0          \n       1   .       R   a
0000440   6       Q   b   5       2   .       R   x   a   7       Q   c
0000460   6       3   .       R   a   6       Q   d   7       4   .
0000500   R   a   8   +       R   c   8       5   .       R   x   c   8
0000520   +       Q   x   c   8       6   .       Q   x   d   5       h
0000540   6       7   .       N   f   7   +       K   h   7       8   .
0000560       N   x   e   5       Q   e   8       9   .       Q   b   7
0000600       Q   a   4       1   0   .       b   4       Q   a   2
0000620   1   1   .       B   f   8       Q   d   2       1   2   .
0000640   R   e   3       Q   d   8       1   3   .       B   x   g   7
0000660       N   x   g   7       1   4   .       b   5       Q   d   6
0000700       1   5   .       b   6       Q   b   4       1   6   .
0000720   Q   c   7       1   -   0
0000731

Code:
$ cat samp.pgn

 1... Ne4+ 2. Kb6 Kb8 3. Rc2 Rd6 4. Rc5 f5 5. Ra4 Nf6 6. Ra1 Nd7# 0-1
 1. Qxa4 bxa4 2. Rxb8 Rxb8 3. a6 Rd8 4. a7 Be6 5. a8=Q Rxa8 6. Bxa8 Bxa2 7. Bc6 f5 8. Bxa4 Kf7 9. Ra1 Bd5 10. Bb5 g6 11. Bc4 Ke6 12. Ra7 Bg5 13. Rxh7 Bxd2 14. Bxd5+ Kxd5 15. Rd7+ Ke4 16. Rxd2 c4 17. Rd6 1-0
 1. Ra6 Qb5 2. Rxa7 Qc6 3. Ra6 Qd7 4. Ra8+ Rc8 5. Rxc8+ Qxc8 6. Qxd5 h6 7. Nf7+ Kh7 8. Nxe5 Qe8 9. Qb7 Qa4 10. b4 Qa2 11. Bf8 Qd2 12. Re3 Qd8 13. Bxg7 Nxg7 14. b5 Qd6 15. b6 Qb4 16. Qc7 1-0
$

# 2  
Old 08-15-2014
This will remove all empty lines:

Code:
grep -v '^$' file

# 3  
Old 08-15-2014
What you have does not fit the definition of a text file. A non-empty text file's last character is (by definition) a <newline> character. It is also strange that your shell prompt appeared at the start of the next line after the cat; for common settings of PS1, the prompt should have appeared immediately after the Qc7 1-0 at the end of the last line of output instead of being on the next line.

If you know that you have a file that is missing the terminating <newline> character an easy way to fix it is:
Code:
echo >> file

If you have files where you don't know if the file has its terminating <newline> or not, and only want to add a <newline> to files that do not have the required terminator, you could try something like:
Code:
#!/bin/ksh
f="samp.pgn"
if [ -f "$f" ] && [ -s "$f" ]
then	# file is a regular file with size greater than 0
	(	IFS=""; read -r last <<-EOF
			$(tail -c1 "$f")
		EOF
		# If the last character in the file is a <newline>, $last will
		# expand to an empty string.  Add a <newline> if one was not
		# present.
		[ "$last" != "" ] &&  echo >> "$f"
	)
fi

Note that this will not modify a file if it is not a regular file, is a regular file of size 0, and will not create a file if it doesn't exist.
This User Gave Thanks to Don Cragun For This Post:
# 4  
Old 08-15-2014
Quote:
Originally Posted by achenle
This will remove all empty lines:

Code:
grep -v '^$' file

Since the input is not a text file, the standards do not define the behavior of grep. On any system I've seen, this grep will remove the empty line. But on some systems it will add a trailing <newline> (as desired), and on other systems it will remove the partial line at the end of the file (not what the user wants).

From the description in the 1st message in this thread, it isn't obvious to me that the empty line at the start of the file is a problem. To me it sounded like there was an empty line at the start of the file, but the empty line wasn't the problem.

Making any change to the file in vi (such as x to delete a character and then u to undo the change) and then writing the file will cause most implementations of vi to add the missing trailing <newline>, but again, this is not required by the standards. (The behavior of vi is only specified when the file being edited is a text file.)
# 5  
Old 08-16-2014
Thanks Don Cragun for the help.

I tried the below which worked
Code:
echo >>samp.pgn
sed '1d' samp.pgn >samp1.pgn
mv samp1.pgn samp.pgn

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Match text to lines in a file, iterate backwards until text or text substring matches, print to file

hi all, trying this using shell/bash with sed/awk/grep I have two files, one containing one column, the other containing multiple columns (comma delimited). file1.txt abc12345 def12345 ghi54321 ... file2.txt abc1,text1,texta abc,text2,textb def123,text3,textc gh,text4,textd... (6 Replies)
Discussion started by: shogun1970
6 Replies

2. Shell Programming and Scripting

Problem with while loop reading every line of a text file

Hello, I'm using RHEL 5.1 with bash. How to handle "read" inside while loop reading every line? Please see below: # cat /tmp/passwd_sample CARRJ12:qVSn4ja4mFA72,..:20021:125:JULIAN CARR:/home/everyone:/bin/bash HERCOT01:NK/3j2ZB4ZC7Q:20022:125:TOM HERCOCK:/home/everyone:/bin/bash... (4 Replies)
Discussion started by: reddyr
4 Replies

3. Shell Programming and Scripting

Format problem while converting text file to csv

Hi , I need a help in following scenario.I tried searching in google but couldn't able to find the exact answer. Sorry if i am re-posting already answered query. While i am trying to convert into log file into csv i couldn't able to get the format which i am looking for. I converted file... (4 Replies)
Discussion started by: varmas424
4 Replies

4. UNIX Desktop Questions & Answers

Problem in sorting a text file

Hi; I have a text file like this: 1 10 11 2 3 4 M X Y When I sort it numerically using sort -n, it looks like this: Y X M 1 2 3 4 10 (3 Replies)
Discussion started by: a_bahreini
3 Replies

5. Shell Programming and Scripting

Text file creation problem

Using KSH, I have one text file which just contains a list of distinct references on each line, e.g.; 123456789 987654321 15457544X 164450200 etc. The file will always be called "InputRefs.txt". The number of distinct refs will be different each time. For each line (distinct ref) I... (1 Reply)
Discussion started by: b.hamilton
1 Replies

6. Shell Programming and Scripting

Please help. Complicated text file manipulation problem

Let me try my best to give you a picture of what I'm trying to do. Once again I'm sorry for the essay thats coming up. I programmed a rather large library of script functions to deal with input, displaying ANSI block graphics, playing sounds, and refining the terminal and so on. I also designed... (8 Replies)
Discussion started by: tinman47
8 Replies

7. Shell Programming and Scripting

Format problem of text file

Folks pardon me for trivial question. After searching the entire forum i decided to post this question. I have a file with some numbers with commas like this 123,456,789 If i open this textfile with either notepad or wordpad they ae looking absolutely fine. When I open this with excel file... (13 Replies)
Discussion started by: repinementer
13 Replies

8. Shell Programming and Scripting

Problem inserting text into file after specific line

this is utterly embarassing :( after posting here i revisited my files and found that when i used "vi" instead of a gui based editor, i suddenly found that the indentations were in fact wrong :( sorry about this :( (0 Replies)
Discussion started by: mocca
0 Replies

9. UNIX for Dummies Questions & Answers

Problem working with Pipe Delimited Text file

Hello all: I have a following textfile data with name inst1.txt HDR|ABCD|10-13-2008 to 10-19-2008.txt|10-19-2008|XYZ DTL|H|5464-1|0|02-02-2008|02-03-2008||||F||||||||| DTL|D|5464-1|1|02-02-2008|02-03-2008|1||JJJ DTL|D|5464-1|2|02-02-2008|02-03-2008|1||JJJ... (9 Replies)
Discussion started by: ravi0435
9 Replies

10. Shell Programming and Scripting

PERL: Searching for a string in a text file problem

Looking for a bit of help. I need to search for a string of words, but unfortunately these words are located on separate lines. for example the text output is: United Chanmpions Ronaldo Liverpool Losers Torres and my script code is print("DEBUG - checking file message"); while... (15 Replies)
Discussion started by: meevagh
15 Replies
Login or Register to Ask a Question