Operating System - Linux
1751971 Members
4597 Online
108783 Solutions
New Discussion юеВ

Need help with awk/cut script please, formating columns

 
generic_1
Respected Contributor

Need help with awk/cut script please, formating columns

Hello,
I have data that is formated in columns that looks like the following(note the example data is actaully one full line the web page has wrapped it).

11AF: 10A:1/myhost/0_3_0 10A:1/myhost/0_6_1 7A:1/myhost/0_9_0 7A:1/myhost/0_2_1

The / tend to confuse the shell and i have characaters :, /, and space as delimiters on the same line which is making it tough to put the data into proper format. I do not have control over the input format just as a fyi.

The output format I am trying to get to using the above line example is as follows (the following is on one line as well):

11AF 10A 1 myhost 0_3_0 10A 1 myhost 0_6_1 7A 1 myhost 0_9_0 7A 1 myhost 0_2_1

I am trying to get the flow to work like.
for i in `cat list`
do
each formated line above is equal to a variable.
example a=11AF b=10A c=1 and so on.
This way i can generate commands line by line in with the specific variables and order with syntax in between very easily.

Example command -someoption $b -anotheroption $a


Many Thanks.


7 REPLIES 7
James R. Ferguson
Acclaimed Contributor

Re: Need help with awk/cut script please, formating columns

Hi Jeff:

Perl will handle multiple characters as delimiters.

# X='11AF: 10A:1/myhost/0_3_0 10A:1/myhost/0_6_1 7A:1/myhost/0_9_0 7A:1/myhost/0_2_1'

# echo ${X}|perl -nle '@a=split /[:\/]/;print "@a"'

11AF 10A 1 myhost 0_3_0 10A 1 myhost 0_6_1 7A 1 myhost 0_9_0 7A 1 myhost 0_2_1

If this is what you want, do:

# perl -nle '@a=split /[:\/]/;print "@a"'

Regards!

...JRF...
Ninad_1
Honored Contributor

Re: Need help with awk/cut script please, formating columns

Hi,

Its very simple. Use the following

cat filename | sed -e "s/:/ /g" -e "s/\// /g"

Regards,

Ninad
RAC_1
Honored Contributor

Re: Need help with awk/cut script please, formating columns

One by one, use tr
echo "11AF: 10A:1/myhost/0_3_0 10A:1/myhost/0_6_1 7A:1/myhost/0_9_0 7A:1/myhost/0_2_1" | tr '\:' " " |tr '\/' " "
There is no substitute to HARDWORK
Peter Nikitka
Honored Contributor

Re: Need help with awk/cut script please, formating columns

Hi,

get rid of multiple delimiter and empty fields.
If you should need the original $@ after this processing, store the positional parameters into a differnt variable.
If your strings form lines in the file list:

typeset -i i n=0
sed 's,[:/], ,g' list |
while read line
do
((n+=1))
set $line
# processing as one unit
echo $line
# processing of the fields
for var
do
echo $var
done
# processing as a matrix
i=1
while [ i -le $# ]
do
eval print $n.$i \$$i
done
done

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
john korterman
Honored Contributor

Re: Need help with awk/cut script please, formating columns

Hi,

try the attached script, using your infile as $1

regards,
John K.
it would be nice if you always got a second chance
James R. Ferguson
Acclaimed Contributor

Re: Need help with awk/cut script please, formating columns

Hi (again) Jeff:

Perl's version of 'tr' will do this too, thusly:

# perl -nle 'tr /:\/ / /s;print' filename

[ Please copy-and-paste since the Forum is going to reduce the whitespace above ].

Regards!

...JRF...
TwoProc
Honored Contributor

Re: Need help with awk/cut script please, formating columns

A different take:

When confronted with this type of thing when I 've already got a working solution on one type of delimiter (as you do).

Have a pre-processing pass that will unify your delimiter to one type instead of two.

In your script before running your regular process:

cat file1 | sed -e "s/\//:/g" > file1.a

[The above changes backslashes to ":" - giving you a single delimiter that doesn't need escaping to process]

... continue on with your program reading "file1.a" instead of "file1" - using ":" as the delimiter for parsing your variables.

This is a "cheap" way out with the following obvious limitation: If the file you are parsing is HUGE, then the above solution may cost you too much time, in which case there are better previous postings to do it all it once.
We are the people our parents warned us about --Jimmy Buffett