Getting Same Lines from File(BASH)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Getting Same Lines from File(BASH)
# 1  
Old 08-31-2012
Getting Same Lines from File(BASH)

hey all,

./test
Code:
#!/bin/bash
for line in $(cat data1)
do
echo $line
done

data1
Code:
ello there
nobody says a word

Output after running the file :
Code:
hello
there
nobody
says
a
word

While output should be this :
Code:
hello there
nobody says a word

Could anyone explain/fix this?

I could use other kinds of loops that would do the job! but I am interested in understanding this kind of loop.

thanks a lot.
# 2  
Old 08-31-2012
The input to for is a list of tokens separated by the characters defined in the internal field seperator (IFS) variable; usually space, tab and newline. For example:

Code:
for x in one two three 
do
   echo "$x"
done

Will set the variable x to each of the three tokens on the line and then echo those tokens out, one per line:

Code:
one
two
three

In your example, you use cat to supply the tokens which are space separated words. The shell then runs one iteration of the loop for each of the tokens and as you discovered you end up with one word printed per line.

As you said, there are better ways to do this. For the sake of anybody who comes across this thread unsure of how:

Code:
while read rec
do
   echo "$rec"
done <file

This User Gave Thanks to agama For This Post:
# 3  
Old 08-31-2012
Code:
for line in $(cat data1)...

using for x in... is using values delimited by space, not newline, if you want to use your code as it is you must change the delimiter to newline:
EDIT: IFS seems to be space, tab or newline by default.
Code:
#! /bin/bash
OLDIFS="$IFS"
IFS=$'\n'
for line in $(cat data1); do
  echo $line
done
IFS="$OLDIFS"

(thanks to this post:
Quote:
Originally Posted by Corona688
...
I'm sure there are better ways to achieve the result you want, but you wrote that you want to understand why it's not working as you expected.

Last edited by 244an; 08-31-2012 at 09:33 PM..
This User Gave Thanks to 244an For This Post:
# 4  
Old 08-31-2012
Better way you should use this...

Code:
for line in "$(cat data1)"
do
echo "$line"
done

ello there
nobody says a word

Regards
pamuSmilie

Last edited by pamu; 08-31-2012 at 11:57 PM..
This User Gave Thanks to pamu For This Post:
# 5  
Old 08-31-2012
No, that is a useless use of cat and dangerous use of backticks, and doesn't work properly. It will split on any spaces, not just newlines.
This User Gave Thanks to Corona688 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash : Checking Large file for specific lines

Morning .. I have a file with approximately 1000 lines. I want to check that the file contains, for example, 100 lines. Something like whats given below is ugly. And even if I create a function I have to call it 100 times. I may need to look through multiple files at times. Is there a... (4 Replies)
Discussion started by: sumguy
4 Replies

2. Shell Programming and Scripting

BASH logging to file, limit lines

BASH Gurus: Anyone know how to append continuous output command appending to a file, but limit that file to no more than 20 lines? The program I have running is simply monitoring my UDP port 53 for incoming packets endlessly. I just need to keep this file from going over 20 lines. Once the file... (3 Replies)
Discussion started by: scorpius2k1
3 Replies

3. Shell Programming and Scripting

Bash script - printing range of lines from text file

I'm working on a new exercise that calls for a script that will take in two arguments on the command line (representing the range of line numbers) and will subsequently print those lines from a a specified file. Command line would look like this: ./lines_script.bash 5 15 <file.txt. The script would... (8 Replies)
Discussion started by: ksmarine1980
8 Replies

4. Shell Programming and Scripting

Bash script to send lines of file to new file based on Regex

I have a file that looks like this: cat includes CORP-CRASHTEST-BU e:\crashplan\ CORP-TEST /usr/openv/java /usr/openv/logs /usr/openv/man CORP-LABS_TEST /usr/openv/java /usr/openv/logs /usr/openv/man What I want to do is make three new files with just those selections. So the three... (4 Replies)
Discussion started by: newbie2010
4 Replies

5. Shell Programming and Scripting

bash keep only duplicate lines in file

hello all in my bash script I have a file and I only want to keep the lines that appear twice in the file.Is there a way to do this? thanks in advance! (4 Replies)
Discussion started by: vlm
4 Replies

6. Shell Programming and Scripting

bash - joining lines in a file

I’m writing a bash shell script and I want to join lines together where two variables on each line are the same ie. 12345variablestuff43212morevariablestuff 12345variablestuff43212morevariablestuff 34657variablestuff78945morevariablestuff 34657variablestuff78945morevariablestuff... (12 Replies)
Discussion started by: Cultcha
12 Replies

7. Shell Programming and Scripting

[bash help]Adding multiple lines of text into a specific spot into a text file

I am attempting to insert multiple lines of text into a specific place in a text file based on the lines above or below it. For example, Here is a portion of a zone file. IN NS ns1.domain.tld. IN NS ns2.domain.tld. IN ... (2 Replies)
Discussion started by: cdn_humbucker
2 Replies

8. Shell Programming and Scripting

Help with comparing 2 lines in 2 different file in shell bash

Hi guys i need help with comparing lines in 2 separate files. Both files contain the same amount of lines and i need to output the difference into the 2nd file. The 1st file is always correct. 1st file (Expected.e): Tuesday, 11 August 2009 Wednesday, 12 August 2009 Thursday, 13 August 2009... (2 Replies)
Discussion started by: kcrap
2 Replies

9. Shell Programming and Scripting

How to use while loop in bash shell to read a file with 4 lines of gap

Hi , I am currently using the while loop in bash shell, as follows. while read line do echo $line done < file.txt However, i want to use the while loop on file.txt, which will read the file with 4 lines of gap. Ex- if file.txt is a file of 100 lines, then i want to use the loop such... (3 Replies)
Discussion started by: jitendriya.dash
3 Replies

10. 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
Login or Register to Ask a Question