LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-25-2013, 11:31 PM   #1
casperdaghost
Member
 
Registered: Aug 2009
Posts: 349

Rep: Reputation: 16
for loop concatination


I have too diff a series of files to check the work i did .
Code:
casper@bignit03 user $ cat > /tmp/ccccccc
casper@bignit03 rcam $ sudo cp ~casper/ROLLOUTS/2013-04-25/frodo4131.1.commands /var/ftp/frodo4131.1.commands
casper@bignit03 rcam $ sudo cp ~casper/ROLLOUTS/2013-04-25/frodo4131.2.commands /var/ftp/frodo4131.2.commands
casper@bignit03 rcam $ sudo cp ~casper/ROLLOUTS/2013-04-25/frodo4131.3.commands /var/ftp/frodo4131.3.commands
casper@bignit03 rcam $ sudo cp ~casper/ROLLOUTS/2013-04-25/frodo4131.4.commands /var/ftp/frodo4131.4.commands
casper@bignit03 rcam $ sudo cp ~casper/ROLLOUTS/2013-04-25/frodo4312.2.commands /var/ftp/frodo4312.2.commands
casper@bignit03 rcam $ sudo cp ~casper/ROLLOUTS/2013-04-25/frodo4312.3.commands /var/ftp/frodo4312.3.commands
the files are in the last two fields, so I echo and cut them out

Code:
casper@bignit03 user $ cat /tmp/ccccccc | cut -d " " -f 6,7
~casper/ROLLOUTS/2013-04-25/frodo4131.1.commands /var/ftp/frodo4131.1.commands
~casper/ROLLOUTS/2013-04-25/frodo4131.2.commands /var/ftp/frodo4131.2.commands
~casper/ROLLOUTS/2013-04-25/frodo4131.3.commands /var/ftp/frodo4131.3.commands
~casper/ROLLOUTS/2013-04-25/frodo4131.4.commands /var/ftp/frodo4131.4.commands
~casper/ROLLOUTS/2013-04-25/frodo4312.2.commands /var/ftp/frodo4312.2.commands
~casper/ROLLOUTS/2013-04-25/frodo4312.3.commands /var/ftp/frodo4312.3.commands
but when I echo them out in a script - they get seperated by the for loop

Code:
casper@bignit03 user $
casper@bignit03 user $
casper@bignit03 user $
casper@bignit03 user $ for i in $(cat /tmp/ccccccc | cut -d " " -f 6,7)
> do
> echo "diff $i"
> done
diff ~casper/ROLLOUTS/2013-04-25/frodo4131.1.commands
diff /var/ftp/frodo4131.1.commands
diff ~casper/ROLLOUTS/2013-04-25/frodo4131.2.commands
diff /var/ftp/frodo4131.2.commands
diff ~casper/ROLLOUTS/2013-04-25/frodo4131.3.commands
diff /var/ftp/frodo4131.3.commands
diff ~casper/ROLLOUTS/2013-04-25/frodo4131.4.commands
diff /var/ftp/frodo4131.4.commands
diff ~casper/ROLLOUTS/2013-04-25/frodo4312.2.commands
diff /var/ftp/frodo4312.2.commands
diff ~casper/ROLLOUTS/2013-04-25/frodo4312.3.commands
diff /var/ftp/frodo4312.3.commands
casper@bignit03 user $
instead of :

Code:
diff ~casper/ROLLOUTS/2013-04-25/frodo4312.3.commands
diff /var/ftp/frodo4312.3.commands

Code:
i want
diff ~casper/ROLLOUTS/2013-04-25/frodo4312.3.commands /var/ftp/frodo4312.3.commands
 
Old 04-25-2013, 11:40 PM   #2
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
Problem is because you're doing echo "diff $i" which will not fetch difference between two files, but just print what's next to echo statement. So, instead of that, simply do:
Code:
diff $i
And script should look like:
Code:
while read -r files; do
diff $files
done < $(cut -d" " -f6,7 /tmp/ccccccc)

Last edited by shivaa; 04-25-2013 at 11:42 PM.
 
Old 04-25-2013, 11:53 PM   #3
casperdaghost
Member
 
Registered: Aug 2009
Posts: 349

Original Poster
Rep: Reputation: 16
I like were we are going with this....while loops.
I get an error

Code:
casper@bigserver03 user $ while read -r files; do diff $files; done < $(cut -d" " -f6,7 /tmp/ccccccc
-bash: $(cut -d" " -f6,7 /tmp/ccccccc): ambiguous redirect
Code:
casper@bigserver03 user $ while read -r files; do diff $files; done < $(more  /tmp/ccccccc | cut -d" " -f6,7)
-bash: $(more  /tmp/ccccccc | cut -d" " -f6,7): ambiguous redirect
casper@bigserver03 user $
 
Old 04-26-2013, 12:08 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,425

Rep: Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823
Process substitution does not require the dollar sign:
Code:
done < <(commands here)
This will actually still not work, so I suggest you use the old -xv to see why

Last edited by grail; 04-26-2013 at 12:09 AM.
 
Old 04-26-2013, 12:10 AM   #5
casperdaghost
Member
 
Registered: Aug 2009
Posts: 349

Original Poster
Rep: Reputation: 16
oh so many riddles. and you assume that i am old, and that i know what I am doing
what is the "old-xv" and where does it goes.
 
Old 04-26-2013, 12:32 AM   #6
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
It's a script, not one liner command. As far as I know while loop can't read line by line if it's used in one liner. Instead create a script and then try again:
Code:
#!/bin/bash
while read -r files; do
diff $files
done < $(cut -d" " -f6,7 /tmp/ccccccc)
For one liner, you can try:
Code:
for files in $(cut -d" " -f6,7 /tmp/ccccccc); do diff $files; done
 
Old 04-26-2013, 01:44 AM   #7
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 840

Rep: Reputation: 380Reputation: 380Reputation: 380Reputation: 380
Quote:
Originally Posted by shivaa View Post
As far as I know while loop can't read line by line if it's used in one liner
What do you mean? Why wouldn't while+read work in a one liner? Can you show an example?

Quote:
Originally Posted by shivaa View Post
For one liner, you can try:
Code:
for files in $(cut -d" " -f6,7 /tmp/ccccccc); do diff $files; done
You definitely shouldn't read the file with for. It will read the input word by word rather than line by line.
 
Old 04-26-2013, 01:54 AM   #8
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
Quote:
Originally Posted by millgates View Post
What do you mean? Why wouldn't while+read work in a one liner? Can you show an example?
I didn't try it, so I am not sure whether it will work or not. OP can give a try to while+read one liner also.
 
Old 04-26-2013, 02:42 AM   #9
casperdaghost
Member
 
Registered: Aug 2009
Posts: 349

Original Poster
Rep: Reputation: 16
i am intrigued by the 'old -xv' - google is really tough to look up command flags .
for is going to delimits list by spaces - while can read a line.

That is something to think about*

reading lines, not delimiting a list by spaces.

i am almosting.
 
Old 04-26-2013, 03:06 AM   #10
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,260

Rep: Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328
Code:
set -xv
at the top of your script causes the parser (ie bash) to show you exactly what its reading and what it generates ie debugs your code; highly informative.

You do need line-by-line reading, hence using a while loop with input defined at the end.
A for loop like that defaults to supplying input a as a separate item for each space separated field.
You can set the IFS, but be careful to set it only where you need it
Code:
old_ifs=$IFS
IFS=$'\n'
for rec in $(cat file)
do
    stuff
done
IFS=$old_ifs
 
Old 04-26-2013, 11:52 AM   #11
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,425

Rep: Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823
Sorry ... didn't get back to a computer until now, but I see chrism01 has provided the good oil on -xv. Also, apologies if I implied age, I was more referring to your age
at the site and thought you may have come across this switch / setting previously

I am also pro the while loop for reading files but after applying the switches mentioned you will see why the current process is still flawed.
 
Old 04-29-2013, 09:58 AM   #12
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958
Please, please, do not use a for loop for this. Reading lines of data is what a while+read loop is designed for.

And we don't need cut either, since read can do all the splitting for you.

Code:
while read -r -a files || [[ -n $files ]]; do

    diff "${files[5]}" "${files[6]}"

done </tmp/ccccccc
(Remember, arrays are zero-indexed.)

You could also use a disposable variable to ignore the fields you don't want, instead of an array. This makes it portable to posix-shells as well.

Code:
while read -r _ _ _ _ _ file1 file2 _ || [ -n "$file1" ]; do

    diff "$file1" "$file2"

done </tmp/ccccccc
Edit: added a simple test to handle the possibility of the file having no terminating newline.

Last edited by David the H.; 04-29-2013 at 10:08 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to loop over text file lines within bash script for loop? johnpaulodonnell Linux - Newbie 9 07-28-2015 04:49 PM
What does GCC/C think of operator + when used for concatination? TheIndependentAquarius Programming 7 03-15-2013 12:21 PM
[SOLVED] Bash - While Loop reading from two lists simultaneously - nested while loop wolverene13 Programming 11 10-01-2011 06:00 PM
File name manipulation with tr, concatination troubles goofyheadedpunk Programming 9 07-06-2004 03:39 AM


All times are GMT -5. The time now is 03:39 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration