Issue with cat command on a for loop

Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Issue with cat command on a for loop
# 1  
Old 08-20-2019
Issue with cat command on a for loop

Good day to all,

I'd like to ask for your advice with regards to this.

Scenario :
I have here a file named TEST.tmp wherein the value inside is below;

But when I'm trying to use this in a simple command like;

for TESTING in $(cat TEST.tmp)
 FIRSTID=$(echo -e "${TESTING}")
echo -e "$FIRSTID" >> TESTING.txt

The output I am getting is this;

I'm not sure as to why it is printing the NINE value on a new line.

Please help. Thank you in advance.

Last edited by rbatte1; 08-21-2019 at 10:36 AM.. Reason: Spelling
# 2  
Old 08-20-2019
This is because the for list splits on IFS that defaults to space,tab,NL.
So a space is treated like a NL.
You can explicitly set IFS to only NL
# subshell starts
# split on NL only
for TESTING in $(cat TEST.tmp); do echo "$TESTING"; done
# subshell ended, IFS is default again.

But it is more clever to use read that reads lines
while read line
  echo "$line"
done < TEST.tmp

And it splits into distinct variables by IFS (space and tabs only; it will never see an NL because of the read)
while read word1 word2
  echo "word1=$word1 word2=$word2"
done < TEST.tmp

This User Gave Thanks to MadeInGermany For This Post:
# 3  
Old 08-21-2019
for TESTING in $(<TEST.tmp); do
        FIRSTID=$(echo -e "$TESTING")
        echo -ne "$FIRSTID"
done > TESTING.txt

# 4  
Old 08-22-2019
HI MadeInGermany,

Thank you so much, that worked!
# 5  
Old 08-22-2019
for TESTING in "$(cat TEST.tmp)"
FIRSTID=$(echo -e "${TESTING}")
echo -e "$FIRSTID"
done >> TESTING.txt

Last edited by rdrtx1; 02-18-2020 at 09:27 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script with ^M causes display issue with cat or more

I have a script to do a couple simple but repetitive commands on files that are provided to us. One of the things is to get rid of the line feeds. This is the section that is causing problems, i even cut this section into its own file to make sure nothing else was affecting it. #!/usr/bin/bash... (4 Replies)
Discussion started by: oly_r
4 Replies

2. Shell Programming and Scripting

For loop, awk command issue

limit.csv data -------------- 5600050 38Nhava 400077 27Bomay rate.txt data ------------- 38NhaVA 27BomaY 27Bomay below is my script: for i in `cat limit.csv` do b=`awk '{print $1}' $i` (4 Replies)
Discussion started by: p_satyambabu
4 Replies

3. Shell Programming and Scripting

for loop with whole line using cat

Hi all, I need to create loop script to read full line and append a variable to each line. cat file I need the output like below,136 1 24 048800 id N4 No_Light,137 1 25 048900 id N4 No_Light,140 1 28 048c00 id N4 No_Light,262 1 38 048e80... (13 Replies)
Discussion started by: ranjancom2000
13 Replies

4. Shell Programming and Scripting

Issue in cat command

We have shell script (in ksh) which reads the records from a csv file line by line and does some operation. We have below command to read the file (CSV has the absolute path of files stored on a server something like SER/IMP/TEST/2010/12/123465.1). P_FILES=`cat $P_BATCH_FILE` for i in $P_FILES... (2 Replies)
Discussion started by: shreevatsau
2 Replies

5. Shell Programming and Scripting

simple for loop/cat issue

ok.. so problem is: I have a file that reads: cat 123 1 and 2 3 and 4 5 and 6 I was using for loops to run through this information. Code: for i in `cat 123` do echo $i done shouldn't the output come as 1 and 2 (3 Replies)
Discussion started by: foal_11
3 Replies

6. Emergency UNIX and Linux Support

cat issue

I have list of files in my current directory abc.txt 123.csv 234.csv 245.csv 145.csv I don't want to open first and last file. i.e (abc.txt and 123_ex_csv) I tried cat *.csv, but it won't work. Can anyone tell me the proper regex only in cat Thanks Pritish ... (2 Replies)
Discussion started by:
2 Replies

7. 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

8. Shell Programming and Scripting

cat and loop

Hi I have a simple code that I want to execute. out=out.txt for f in `cat list.txt | head -1`; do echo $f >> $out echo "sleep 5" >> $out done cat list.txt | head -1 wget -q -O - ' cat out.txt wget sleep 5 -q sleep 5 -O (10 Replies)
Discussion started by: soemac
10 Replies

9. Shell Programming and Scripting

Issue with Unix cat command

Hi Experts, I am finding the performance of cat command is very wierd, it is taking more time to merge the files into a single file. We have a situation where we would be merging more than 100 files into a single file, but with cat command it is running slow. I tried doing with paste, join... (13 Replies)
Discussion started by: RcR
13 Replies

10. UNIX for Dummies Questions & Answers

Using cat command in a for loop

If I have a data file containing entries like-> abc abc:123 and I use a for loop: for I in `cat data-file` do echo $I done the output would contain 2 lines -> abc.... and abc:123 but I want it to be on only one line. How can I do this? thanks (1 Reply)
Discussion started by: sleepster
1 Replies
Login or Register to Ask a Question

Featured Tech Videos