Unix Shell - simple awk script to separate white spaces

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > February 2007 > simple awk script to separate white spaces





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author simple awk script to separate white spaces
bhaveshah@gmail.com

2007-02-22, 7:15 pm

Hi Gurus,
I have a text file with multiple entries as below. some starts/ends
with double quotes some doesn't. Also some have space in-between. I
need to create a directory for each left column and copy data from
each corrosponing right column to the directory on left. As the file
doesn't have quotes

e.g.
cp -rp /2001/01 "test/test 1/test2"

the problem is awk breaks each space and chops the left side quotes.
like it creates "test/2425 - EU HE Study/200" in to "test/2425
EU
HE
Study"

cat sample.txt
test/test1 /2001
"test/test 1/test2" /2001/01
"test/test - 23 -en/test3" /2002/03
"test/2425 - EU HE Study/200 /ms2

for file in `cat sample.txt`
do
srcdir=$(echo "$file" | awk '{print $1}')
srcpath=$(echo $file | awk '{print $2}')
echo $srcdir
echo $srcpath
mkdir -p $srcdir
cp -rp $srcpath $srcdir
done

Ed Morton

2007-02-23, 1:21 am

bhaveshah@gmail.com wrote:
> Hi Gurus,
> I have a text file with multiple entries as below. some starts/ends
> with double quotes some doesn't. Also some have space in-between. I
> need to create a directory for each left column and copy data from
> each corrosponing right column to the directory on left. As the file
> doesn't have quotes
>
> e.g.
> cp -rp /2001/01 "test/test 1/test2"
>
> the problem is awk breaks each space and chops the left side quotes.
> like it creates "test/2425 - EU HE Study/200" in to "test/2425
> EU
> HE
> Study"
>
> cat sample.txt
> test/test1 /2001
> "test/test 1/test2" /2001/01
> "test/test - 23 -en/test3" /2002/03
> "test/2425 - EU HE Study/200 /ms2
>
> for file in `cat sample.txt`
> do
> srcdir=$(echo "$file" | awk '{print $1}')
> srcpath=$(echo $file | awk '{print $2}')
> echo $srcdir
> echo $srcpath
> mkdir -p $srcdir
> cp -rp $srcpath $srcdir
> done
>


a) What do you want to do with the final line of your input file:

"test/2425 - EU HE Study/200 /ms2

where there's an unmatched quote?

b) What if your directory name contained a double quote?

c) What if your directory name contained a newline?

Ed.
explor

2007-02-23, 1:21 am

Thanks Ed for your quick response.

here is my response...
a) What do you want to do with the final line of your input file:
>
> "test/2425 - EU HE Study/200 /ms2


Every line shoud do the same thing - create a directory with mkdir -p
"test/2425 - EU HE Study/200" ; cp -rp /ms2 "test/2425 - EU HE Study/
200"

> where there's an unmatched quote?


No there is no unmatched quote.
>
> b) What if your directory name contained a double quote?

Directory name doesn't contain double quotes
>
> c) What if your directory name contained a newline?

Direcotry name doesn't contain new line.

>
> Ed.- Hide quoted text -
>
> - Show quoted text -



Janis

2007-02-23, 7:21 am

On 23 Feb., 00:31, bhaves...@gmail.com wrote:
> Hi Gurus,
> I have a text file with multiple entries as below. some starts/ends
> with double quotes some doesn't. Also some have space in-between. I
> need to create a directory for each left column and copy data from
> each corrosponing right column to the directory on left. As the file
> doesn't have quotes
>
> e.g.
> cp -rp /2001/01 "test/test 1/test2"
>
> the problem is awk breaks each space and chops the left side quotes.


Since your second column seems separated by space sequences and a
slash you can change the separator to something unique and completely
remove the quotes

sed -e 's+ */+|/+' -e 's/"//g'

The result may be processed with awk as you like, just define the FS
appropriately (as "|" in the above example). (Of course you can also
implement the sed functions completely within your awk script.)

Other options are to parse the "first column" yourself with awk.
/".*"/ { ...extract anything between the quotes as var1... ; var2 =
$NF }
!/".*"/ { var1 = $1 ; var2 = $NF }
The extraction might be as simple as
var1=$0; sub(/.*"/,"",var1); sub(/".*/,"",var1)

(all untested)

Janis

> like it creates "test/2425 - EU HE Study/200" in to "test/2425
> EU
> HE
> Study"
>
> cat sample.txt
> test/test1 /2001
> "test/test 1/test2" /2001/01
> "test/test - 23 -en/test3" /2002/03
> "test/2425 - EU HE Study/200 /ms2
>
> for file in `cat sample.txt`
> do
> srcdir=$(echo "$file" | awk '{print $1}')
> srcpath=$(echo $file | awk '{print $2}')
> echo $srcdir
> echo $srcpath
> mkdir -p $srcdir
> cp -rp $srcpath $srcdir
> done



Dan Mercer

2007-02-23, 1:18 pm


<bhaveshah@gmail.com> wrote in message news:1172187089.671720.195800@h3g2000cwc.googlegroups.com...
: Hi Gurus,
: I have a text file with multiple entries as below. some starts/ends
: with double quotes some doesn't. Also some have space in-between. I
: need to create a directory for each left column and copy data from
: each corrosponing right column to the directory on left. As the file
: doesn't have quotes
:
: e.g.
: cp -rp /2001/01 "test/test 1/test2"
:
: the problem is awk breaks each space and chops the left side quotes.
: like it creates "test/2425 - EU HE Study/200" in to "test/2425
: EU
: HE
: Study"
:
: cat sample.txt
: test/test1 /2001
: "test/test 1/test2" /2001/01
: "test/test - 23 -en/test3" /2002/03
: "test/2425 - EU HE Study/200 /ms2
I assume this is a typo

You don't need awk - you can't do it with awk.
:
: for file in `cat sample.txt`

There's a reason we call this a useless use of cat


while IFS= read line
do
eval set -- $line
echo "<$1><$2>"
done <<!
"Now is the winter of our discontent" "made glorious Summer by this Sun of York"
!
<Now is the winter of our discontent><made glorious Summer by this Sun of York>

The rest is left as an exercise for the student

Dan Mercer

: do
: srcdir=$(echo "$file" | awk '{print $1}')
: srcpath=$(echo $file | awk '{print $2}')
: echo $srcdir
: echo $srcpath
: mkdir -p $srcdir
: cp -rp $srcpath $srcdir
: done
:


explor

2007-02-23, 1:18 pm

Hi Dan,
Thanks..
Once you get <Now is the winter of our discontent><made glorious
Summer by this Sun of York> , how do you separate columns from <>?

> You don't need awk - you can't do it with awk.
> :
> : for file in `cat sample.txt`
>
> There's a reason we call this a useless use of cat
>
> while IFS= read line
> do
> eval set -- $line
> echo "<$1><$2>"
> done <<!
> "Now is the winter of our discontent" "made glorious Summer by this Sun of York"
> !
> <Now is the winter of our discontent><made glorious Summer by this Sun of York>
>


>
> : do
> : srcdir=$(echo "$file" | awk '{print $1}')
> : srcpath=$(echo $file | awk '{print $2}')
> : echo $srcdir
> : echo $srcpath
> : mkdir -p $srcdir
> : cp -rp $srcpath $srcdir
> : done
> :



Dan Mercer

2007-02-26, 1:18 pm


"explor" <bhaveshah@gmail.com> wrote in message news:1172253779.984813.180550@z35g2000cwz.googlegroups.com...
: Hi Dan,
: Thanks..
: Once you get <Now is the winter of our discontent><made glorious
: Summer by this Sun of York> , how do you separate columns from <>?

What do you mean?

Dan Mercer

:
: > You don't need awk - you can't do it with awk.
: > :
: > : for file in `cat sample.txt`
: >
: > There's a reason we call this a useless use of cat
: >
: > while IFS= read line
: > do
: > eval set -- $line
: > echo "<$1><$2>"
: > done <<!
: > "Now is the winter of our discontent" "made glorious Summer by this Sun of York"
: > !
: > <Now is the winter of our discontent><made glorious Summer by this Sun of York>
: >
:
: >
: > : do
: > : srcdir=$(echo "$file" | awk '{print $1}')
: > : srcpath=$(echo $file | awk '{print $2}')
: > : echo $srcdir
: > : echo $srcpath
: > : mkdir -p $srcdir
: > : cp -rp $srcpath $srcdir
: > : done
: > :
:
:


Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2009 webservertalk.com