Re: OT: a problem about text manipulation

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 11/4/06, Les Mikesell <lesmikesell@xxxxxxxxx> wrote:
> > > >I am looking for an automatic way of transforming my text file into
> > > >another one with the format
> > > You may try this:
> > >  cat ./test.txt | awk '{print $1" "$2; print $1" "$3; print $1" "$4}' |
> > >  grep -v ' $'
> >
> > $ awk '{for(i=2;i<=NF;i++)printf $1"\t"$i"\n"}' test.txt
> >
> > That's all.
> >
> > or, try this.
> >
> > $ awk '{for(i=2;i<=NF;i++)print $1, $i}' test.txt
> >
> > I think awk is the best solution for this problem.
>
> The following command does almost exactly what I want:
>
> cat ./filename_introduced_user.txt | awk '{print $1" "$2; print $1"
> "$3; print $1" "$4; print $1" "$5; print $1" "$6; print $1" "$7; print
> $1" "$8; print $1" "$9; print $1" "$10; print $1" "$11; print $1"
> "$12; print $1" "$13; print $1" "$14; print $1" "$15; print $1" "$16;
> print $1" "$17; print $1" "$18; print $1" "$19; print $1" "$20; print
> $1" "$21; print $1" "$22; print $1" "$23; print $1" "$24; print $1"
> "$25; print $1" "$26; print $1" "$27}' | grep -v ' $' >
> another_filename_introduced_user.txt
>
> I am wondering whether it is possible to write a script to do the same
> but for a number of columns introduced by the user.
>
> Thanks in advance,

If you want to adapt to the columns in the file, this
would work:

#!/bin/sh
while read LINE
do
 set -- $LINE
 N=$1
 while shift
 do
  if [ -n "$1" ]
  then
   echo $N $1
   fi
 done
done
                                                                                If you want to set a limit on the columns you could add a
counter on the loop doing the shift and pass a value in
on the command line.

Thanks, Les. I have meanwhile learned a bit of Ruby and I could do the
same with it.

Paul


[Index of Archives]     [Current Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Yosemite News]     [Yosemite Photos]     [KDE Users]     [Fedora Tools]     [Fedora Docs]

  Powered by Linux