Cat Command and Blank Lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Cat Command and Blank Lines
# 8  
Old 02-26-2010
If you really want to use cat:

Code:
cat file | while read line 
do
echo "$line"
if [ -z "${line}" ]; then 
 echo "Blank line"
fi
done

cheers,
Devaraj Takhellambam
# 9  
Old 02-26-2010
Further to abubacker and for completeness.
We put $groupline in double quotes or we get a syntax error from the if statement if the line is blank.
As other posters note the "for" method loses the blank line completely when expanding the output from "cat" on the command line.
The "while read" construct is preferred because it can deal with any number of records whereas the "for" method could expand to a command line which fails because it is too long.

A version with "cat":

Code:
cat /home/test/group | while read groupline
do
         if [ -z "${groupline}" ]
             then
             echo "blank found"
         fi 
done


Last edited by methyl; 02-26-2010 at 07:57 AM.. Reason: Layout
# 10  
Old 02-26-2010
if you can insert a -n into the cat statement you can work out from the line numbers if a line is blank.
# 11  
Old 02-26-2010
Tools

What about this?
Code:
cat myfile | od -An -t dC -w10

Which will provide the decimal ASCII representation of what is on a line.

Does that help you?
# 12  
Old 02-27-2010
cat with blank lines

Dear Friend,

If you want to print the file content with the empty line, you can use the -A option with cat command.

I have written the following code. It is print the empty line also. But it will print the empty line as $.

The file content is
welcome

welcome 1

welcome 2

Code:
 
for line in `cat -A 1`
do
        echo $line;
done

The output is
welcome$
$
welcome1$
$
welcome2$
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

To check Blank Lines, Blank Records and Junk Characters in a File

Hi All Need Help I have a file with the below format (ABC.TXT) : ®¿¿ABCDHEJJSJJ|XCBJSKK01|M|7348974982790 HDFLJDKJSKJ|KJALKSD02|M|7378439274898 KJHSAJKHHJJ|LJDSAJKK03|F|9898982039999 (cont......) I need to write a script where it will check for : blank lines (between rows,before... (6 Replies)
Discussion started by: chatwithsaurav
6 Replies

2. UNIX for Advanced & Expert Users

Delete blank spaces and blank lines in a file

Hi Gurus, Somebody can say me how to delete blank spaces and blank lines in a file unix, please. Thank you for advanced. (10 Replies)
Discussion started by: systemoper
10 Replies

3. UNIX for Dummies Questions & Answers

Remove blank lines using cat command

plz help me to figure it out how i remove empty or blank files using cat command. i will be very thankful if u send me this answer... thanks (3 Replies)
Discussion started by: mushfiks1
3 Replies

4. Shell Programming and Scripting

UNIX command to count blank lines in a file in DOS format

Hi Team, The content of the file is as follows. asdf 234 asdf asdf dsfg gh 67 78 The file is in DOS format (not in Unix Format). The file is transferred to Unix. I need a unix command to check the number of blank lines in a input (comming from Windows). If it is greater than... (4 Replies)
Discussion started by: kmanivan82
4 Replies

5. UNIX for Dummies Questions & Answers

Cat command drops lines in output file

I use the cat command to concatenate text files, but one of the rows I was expecting doesn't display in the output file. Is there a verbose mode\logging mechanism for the cat command to help me investigate where the lines I was expecting are going?? cat 7760-001_1_*_06_*.txt | grep -v... (1 Reply)
Discussion started by: Xin Xin
1 Replies

6. Shell Programming and Scripting

Want non-interpretation of blank space using cat

I have a file say ADCD which is like following--> Please consider 'z' as space #cat ADCD <!--Yzzz|z--> <!--Nzzzzz--> Now I want to store the content of this file to a variable say VAR like this--> #VAR=`cat ADCD` #echo $VAR <!--Yz|z--> <!--Nz--> Now I don' t want the variable... (2 Replies)
Discussion started by: muchyog
2 Replies

7. Shell Programming and Scripting

Cat Command on File not printing "Blank" Lines?

Hello All, I have a bash script and in it at some point I call an Expect Script that does some stuff and saves its output in a ".txt" file. Example "/path/to/my/file/Expect_Output.txt" file: notice the 2nd line is empty in the file... Data for Host-1 (192.168.1.110) Checking the... (2 Replies)
Discussion started by: mrm5102
2 Replies

8. Shell Programming and Scripting

Delete blank lines, if blank lines are more than one using shell

Hi, Consider a file named "testfile" The contents of file are as below first line added for test second line added for test third line added for test fourth line added for test fifth line added for test (5 Replies)
Discussion started by: anil8103
5 Replies

9. UNIX for Dummies Questions & Answers

Grep command to remove blank lines

The following grep command grep -v "^$" filename > newfilename does not populate the new file with any data. I see it search the entire input file but the output file never gets filled. Is this not the correct command for what Im looking to do? (2 Replies)
Discussion started by: aispg8
2 Replies

10. Shell Programming and Scripting

cat in the command line doesn't match cat in the script

Hello, So I sorted my file as I was supposed to: sort -n -r -k 2 -k 1 file1 | uniq > file2 and when I wrote > cat file2 in the command line, I got what I was expecting, but in the script itself ... sort -n -r -k 2 -k 1 averages | uniq > temp cat file2 It wrote a whole... (21 Replies)
Discussion started by: shira
21 Replies
Login or Register to Ask a Question