Operating System - HP-UX
1845673 Members
3279 Online
110247 Solutions
New Discussion

cut command in admin script

 
SOLVED
Go to solution
Michael Murphy_2
Frequent Advisor

cut command in admin script

I am using the cut command in a admin script to parse through a file with delimeter of ":". If I return one field from a file I just get the field itself. When I return two fields as in: cut -d : -f 1,2 I get the fields back as well as the delimeter seperating them. Is this expected - is there a way to get just the fields back?
7 REPLIES 7
Thierry Poels_1
Honored Contributor

Re: cut command in admin script

hi,

this is normal behaviour.

Workarounds?
- two separate cut commands?
- translate (tr) the delimeter in the result to something else?
- ...

good luck,
Thierry.
All unix flavours are exactly the same . . . . . . . . . . for end users anyway.
RAC_1
Honored Contributor
Solution

Re: cut command in admin script

As told, this is normal behaviour.
Options -tr and awk.

awk -F : '{print $1, $2}' input_file

Anil
There is no substitute to HARDWORK
Rodney Hills
Honored Contributor

Re: cut command in admin script

How about "awk"

awk -F: 'print $1,$2' yourfile

HTH

-- Rod Hills
There be dragons...
A. Clay Stephenson
Acclaimed Contributor

Re: cut command in admin script

This is normal cut behavior. A better tool is awk:

cat myfile | awk -F '.' '{ print $1,$2 }'

should do just what you want; more over if you would like to read them into 2 shell variables in a loop that is easy as well.

cat myfile | awk -F '.' '{ print $1,$2 }' | while read F1 F2
do
echo "F1 = ${F1} F2 = ${F2}"
done
If it ain't broke, I can fix that.
Michael Schulte zur Sur
Honored Contributor

Re: cut command in admin script

Hi,

use awk -F: '{print $1" "$2}' file

greetings,

Michael
Stuart Whitby
Trusted Contributor

Re: cut command in admin script

You need to use "\:" instead of just ":". The command should work fine.
A sysadmin should never cross his fingers in the hope commands will work. Makes for a lot of mistakes while typing.
Michael Murphy_2
Frequent Advisor

Re: cut command in admin script

Thanks one and all...