Cat Command on File not printing "Blank" Lines?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Cat Command on File not printing "Blank" Lines?
# 1  
Old 07-26-2012
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...
Code:
Data for Host-1 (192.168.1.110)

Checking the status...
Status is GOOD

Then later on in my Bash script, I run the following command to put all the contents of the file
into an array, line-by-line...
Code:
EXPECT_OUTPUT="/path/to/my/file/Expect_Output.txt"

outputArray=( $(cat "$EXPECT_OUTPUT") )

I thought the contents of the array should be this:
(0) = "Data for Host-1 (192.168.1.110)"
(1) = ""
(2) = "Checking the status..."
(3) = "Status is GOOD"

But when I loop through the array and echo out the data I get the following:
Code:
for ((x=0; x <= ${#outputArray[@]}; x++))
 do
    echo "${outputArray[$x]}"
done




_____OUTPUT_____
Data for Host-1 (192.168.1.110)
Checking the status...
Status is GOOD

Does anyone know why it wouldn't retain the empty line when saving into the array?
Any thoughts would be much appreciated...!


Thanks in Advance,
Matt
# 2  
Old 07-26-2012
Where the shell splits in a string is controlled by the IFS variable.

By default, it splits on any whitespace.

Do IFS=$'\n' to make it split on newlines.

Really though, do you actually need to store this file in an array? There's probably better ways to do what you want.
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 07-26-2012
Hey Corona, thanks for the reply!
And thanks AGAIN for answering another one of my questions so quickly...

Duhh... I completely forgot about the IFS.
That should fix it, thanks!


Thanks Again,
Matt
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

2. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

3. Shell Programming and Scripting

Find lines with "A" then change "E" to "X" same line

I have a bunch of random character lines like ABCEDFG. I want to find all lines with "A" and then change any "E" to "X" in the same line. ALL lines with "A" will have an "X" somewhere in it. I have tried sed awk and vi editor. I get close, not quite there. I know someone has already solved this... (10 Replies)
Discussion started by: nightwatchrenba
10 Replies

4. UNIX for Dummies Questions & Answers

Unix "look" Command "File too large" Error Message

I am trying to find lines in a text file larger than 3 Gb that start with a given string. My command looks like this: $ look "string" "/home/patrick/filename.txt" However, this gives me the following message: "look: /home/patrick/filename.txt: File too large" So, I have two... (14 Replies)
Discussion started by: shishong
14 Replies

5. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

6. Shell Programming and Scripting

Cat Command and Blank Lines

Hi All, I was testing for blank lines and I want to use the cat command only for groupline in `cat /home/test/group` do if then echo "blank found" fi done I want to check if the current line read is a blank line. I have tested with $groupline="\n" ,... (11 Replies)
Discussion started by: datkan
11 Replies

7. Shell Programming and Scripting

cat $como_file | awk /^~/'{print $1","$2","$3","$4}' | sed -e 's/~//g'

hi All, cat file_name | awk /^~/'{print $1","$2","$3","$4}' | sed -e 's/~//g' Can this be done by using sed or awk alone (4 Replies)
Discussion started by: harshakusam
4 Replies

8. Shell Programming and Scripting

Reading lines from a file, using bash, "at" command

Hi. I have the script shown below. If I execute it form the command line it seems to work properly, but when I fun it using the unix "at" command "at -m now < ./kill-at-job.sh" It appears to hang. Below is the script, the input file, and the execution as reported in the e-mail from the "at"... (3 Replies)
Discussion started by: jbsimon000
3 Replies

9. Shell Programming and Scripting

"BOLD" printing a variable in PS1 command

I would like to "BOLD" print the hostname in the following statement: export PS1=$USER"@"$(hostname -s):'$PWD>' Is there a special character I can put before and after the variable to make it bold or blinking? Thanks. (4 Replies)
Discussion started by: pdtak
4 Replies

10. UNIX for Dummies Questions & Answers

grep/cat/more -- search in a txt file and display content from a specific "keyword"

Hi, I have a .txt file Sample: ===================== NEXT HOST ===================== AEADBAS001 ip access-list extended BLA_Incoming_Filter ip access-list extended BLA_Outgoing_Filter access-list 1 permit xxxxxxxxxxxxxx access-list 2 permit xxxxxxxxxxxxxx =====================... (4 Replies)
Discussion started by: I-1
4 Replies
Login or Register to Ask a Question