Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with a couple of ??? using sed?

Status
Not open for further replies.

rkckjk

IS-IT--Management
Apr 27, 2001
34
US
First, I have the following tab delimited file:

155.66.220.46 NTADTH0791M00 None wst None "Smith, Scott" 3786
123.66.201.12 NTADTH0802M00 None WST None "Smith, Scott" 4607 "Thomas, John"
166.67.201.13 NTADTH0803M00 None WST None "Smith, Scott" 4607 "Thomas, John"
111.30.220.109 NTADTH0904M00 Standard; Prime Shift Monitoring WST None
189.30.221.148 NTADTH0965M00 Standard; Prime Shift Monitoring WST None

I would like to strip off the first tab delimited field using 'sed', so the file would look like this:

NTADTH0791M00 None wst None "Smith, Scott" 3786 "Thomas, John" 1032
NTADTH0802M00 None WST None "Smith, Scott" 4607 "Thomas, John" 3786 SAN
NTADTH0803M00 None WST None "Smith, Scott" 4607 "Thomas, John" 3786 SAN
NTADTH0904M00 Standard; Prime Shift Monitoring WST None "Thomas, John"
NTADTH0965M00 Standard; Prime Shift Monitoring WST None "Thomas, John" 3786

Secondly, how could I swap the first two fields using 'sed' from the file on top so it would look like this:

NTADTH0791M00 155.66.220.46 None wst None "Smith, Scott" 3786
NTADTH0802M00 123.66.201.12 None WST None "Smith, Scott" 4607 "Thomas, John"
NTADTH0803M00 166.67.201.13 None WST None "Smith, Scott" 4607 "Thomas, John"
NTADTH0904M00 111.30.220.109 Standard; Prime Shift Monitoring WST None
NTADTH0965M00 189.30.221.148 Standard; Prime Shift Monitoring WST None "Thomas, John"

thanks
 
If you don't mind awk, try these solutions:

Code:
# strips off the first field

BEGIN { FS = "\t" }

{ 
    for (i = 2; i <= NF; i ++)
        printf "%s\t", $i
    printf "\n"
}

Code:
# swaps the first two fields

BEGIN { FS = "\t" }

{ 
    printf "%s\t", $2
    printf "%s\t", $1
    for (i = 3; i <= NF; i ++)
        printf "%s\t", $i
    printf "\n"
}


KP.
 
The sed way.
strip off the first tab delimited field
t=`echo "\t"`; sed "s![^$t]*$t!!" /path/to/input > output
swap the first two fields
t=`echo "\t"`; sed "s!\([^$t]*\)$t\([^$t]*\)!\2$t\1!" /path/to/input > output
Another awk way.
strip off the first tab delimited field
awk '{sub(/[^\t]*\t/,"");print}' /path/to/input > output
or
awk '{print substr($0,1+index($0,"\t"))}' /path/to/input > output
swap the first two fields
nawk 'BEGIN{FS=OFS="\t"}{t=$1;$1=$2;$2=t;print}' /path/to/input > output

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
nota: T is a tab-char

s/\(.*\)T\(.*\)T\(.*\)T\(.*\)T\(.*\)T\(.*\)T\(.*\)T\(.*\)/\3T\2T\4T\5T\6T\7T\8/

:) guggach
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top