[CentOS] for loop example

Scott Noel-Hemming scott at viviotech.net
Fri Jun 10 17:43:30 UTC 2016


On Thu, 9 Jun 2016 15:19:57 -0400
Mike Williams <dmikewilliams at gmail.com> wrote:

> > From: Indunil Jayasooriya <indunil75 at gmail.com>
> 
> > > >
> > > > [root at centos67 loop]# cat file1
> > > > firstname1
> > > > firstname2
> > > >
> > > > [root at centos67 loop]# cat file2
> > > > lastname1
> > > > lastname2
> > > >
> > > > I need a OUTPUT like this
> > > >
> > > >
> > > > *firstname1 lastname1firstname2 lastname2*
> > > >
> > > >
> > > > But I try the below command , i get below output.  what is the real
> > command
> > > > to get the above output
> >
> 
> 
> 
> > > >
> > > > [root at centos67 loop]# for line1 in $(cat file1 ); do for line2 in
> > $(cat
> > > > file2 ); do echo "$line1" "$line2";done;done;
> > > > firstname1 lastname1
> > > > firstname1 lastname2
> > > > firstname2 lastname1
> > > > firstname2 lastname2
> > > >
> > > >
> > > > And also, I created a file3 like this
> > > >
> > > > [root at centos67 loop]# cat file3
> > > > firstname1 lastname1
> > > > firstname2 lastname2
> > > >
> > > > How can I get the same OUTPUT ?
> > > >
> > > >
> > > > *firstname1 lastname1firstname2 lastname2*
> > > >
> >
> 
> scott at viviotech.net suggested this:
> 
> > There's probably a better way using join, but this should do the trick:
> 
> > paste <(cat file1 | tr "\n" ' ') <(cat file2 | tr "\n" " ")
> 
> I'd never used the paste command before, so I tried the above oneliner out
> and it did not work on my system. It printed all of file1 followed by all
> of file2.
> 
> An altered version of the paste command did work on my centos 7 system.
> 
> paste -d' ' file1 file2
> Assuming the OP example output was a typo and there should be a space
> between the contents of file1 and file2.
> Otherwise remove or change the -d' '
> 
> Mike
> _______________________________________________
> CentOS mailing list
> CentOS at centos.org
> https://lists.centos.org/mailman/listinfo/centos
> Total Control Panel
> https://asp.reflexion.net/history-detail?hID=15302122680

Interesting, I don't have the same experience: 

 cat /etc/redhat-release 
CentOS Linux release 7.2.1511 (Core) 
[PEXPECT]$ paste -d' ' /tmp/file1 /tmp/file2
firstname1 lastname1
firstname2 lastname2
[PEXPECT]$ paste <(cat /tmp/file1 | tr "\n" ' ') <(cat /tmp/file2 | tr "\n" ' ')
firstname1 firstname2   lastname1 lastname2 

-- 
Nothing Whitty



More information about the CentOS mailing list