Awk Field Seperator Help


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Awk Field Seperator Help
# 1  
Old 11-10-2005
Awk Field Seperator Help

I wrote a script on HPUX 11.11 to turn a Decimal subnet mask (255.255.254.0) to hex 0xfffffe00 (subset of a bigger script). It works great on the HPUX systems but on the freebsd box the awk is not seperating the fields properly. I tried to google for a solution and seaching these forums i am just hoping someone can help. And if anyone knows how i can add the printf lines directly to the end of the awk statements that would be groovy.

I am unsure of the awk version HPUX uses but the freeBSD version is:
awk version 20050424 (FreeBSD), I would prefer not to use nawk or gawk.


#!/bin/sh

echo "Enter Subnet mask:"
read MASK

OCT1=$(echo $MASK|awk '{FS=".";print $1}')
OCT2=$(echo $MASK|awk '{FS=".";print $2}')
OCT3=$(echo $MASK|awk '{FS=".";print $3}')
OCT4=$(echo $MASK|awk '{FS=".";print $4}')

PHEX1=$(printf "%#x\n" $OCT1)
PHEX2=$(printf "%#x\n" $OCT2|sed 's/0x//')
PHEX3=$(printf "%#x\n" $OCT3|sed 's/0x//')
PHEX4=$(printf "%#x\n" $OCT4|sed 's/0x//')

if [ "$PHEX4" = "0" ]
then
PHEX4=00
fi

PHEXMASK=$(echo $PHEX1$PHEX2$PHEX3$PHEX4)

echo $PHEXMASK
# 2  
Old 11-10-2005
Code:
#!/bin/sh

echo "Enter Subnet mask:"
read MASK

oldIFS="${IFS}"
IFS=.

set -- ${MASK}
OCT1=${1}
OCT2=${2}
OCT3=${3}
OCT4=${4}

IFS="${oldIFS}"

PHEX1=$(printf "%#x\n" $OCT1)
PHEX2=$(printf "%#x\n" $OCT2|sed 's/0x//')
PHEX3=$(printf "%#x\n" $OCT3|sed 's/0x//')
PHEX4=$(printf "%#x\n" $OCT4|sed 's/0x//')

if [ "$PHEX4" = "0" ]
then
PHEX4=00
fi

PHEXMASK=$(echo $PHEX1$PHEX2$PHEX3$PHEX4)

echo $PHEXMASK

Cheers
ZB
# 3  
Old 11-10-2005
That worked great bob, I did not even think about using the internal seperator. Thank you for your quick help.
# 4  
Old 11-11-2005
Code:
awk 'BEGIN{FS="."}{printf "0x%02x%02x%02x%02x\n",$1,$2,$3,$4}'

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Field seperator with awk

Hi, input data format: echo ' <APPLICATION="APPLSG" SUB_APPLICATION="DLY" JOBNAME="DPL_BN_RE_CCMS_SA" CMDLINE="run_job.ksh %%PARAM1 %%PARAM2" TASKTYPE="Command" />' expected format: "APPLSG", "DLY", "DPL_BN_RE_CCMS_SA", "run_job.ksh %%PARAM1 %%PARAM2" my command: echo ' ... (2 Replies)
Discussion started by: JSKOBS
2 Replies

2. Shell Programming and Scripting

AWK: Pattern match between 2 files, then compare a field in file1 as > or < field in file2

First, thanks for the help in previous posts... couldn't have gotten where I am now without it! So here is what I have, I use AWK to match $1 and $2 as 1 string in file1 to $1 and $2 as 1 string in file2. Now I'm wondering if I can extend this AWK command to incorporate the following: If $1... (4 Replies)
Discussion started by: right_coaster
4 Replies

3. Shell Programming and Scripting

awk, comma as field separator and text inside double quotes as a field.

Hi, all I need to get fields in a line that are separated by commas, some of the fields are enclosed with double quotes, and they are supposed to be treated as a single field even if there are commas inside the quotes. sample input: for this line, 5 fields are supposed to be extracted, they... (8 Replies)
Discussion started by: kevintse
8 Replies

4. Shell Programming and Scripting

SUBSEP Seperator problem with awk

The following code removes new line with in double quotes I am replacing newline character with in double quotes with 123. intermediatenewline_remover () { typeset Infile=$1 nawk -F"," '{ record = record $0 if ( gsub( /"/, "&", record ) % 2 ) { record = record "123" ... (3 Replies)
Discussion started by: pinnacle
3 Replies

5. Shell Programming and Scripting

field seperator question (awk script)

Is there a way I could use different a different field seperator for different parts of the body? kinda like {FS = ":"} FILENAME == "products"{ price = $3 if(numprods < $1-100) numprods = $1-100 } {FS = "/"}{} FILENAME == "associates"{ associateid... (5 Replies)
Discussion started by: angermanaged
5 Replies

6. Shell Programming and Scripting

Add a field seperator in a file.

"355"|""|"NJ"|"A0A 1W0"|"V"|""|""|""|"N" I've the above sample data seperated with pipe delimeter and in the file I want to replace a space with "|" to the 4th field so the result would be like below. So it would change from 9 fields to 10 fields. "355"|""|"NJ"|"A0A"|"1W0"|"V"|""|""|""|"N" ... (3 Replies)
Discussion started by: rudoraj
3 Replies

7. Shell Programming and Scripting

Printing value with no obvious field seperator

Hi all, Can anybody think of a way to do this? I have a file with content like the following: F_TOP_PARAM_VALUEF_TOP_SOURCEF_TOP_DEL_NOTIFICATIONF_DEST_ADDRF_TOP_DEL_TYPE What I want to do is print out the value in the square brackets after F_TOP_SOURCE. So in this case I'd like to print... (4 Replies)
Discussion started by: Donkey25
4 Replies

8. Shell Programming and Scripting

How to change field seperator

Hi Please help me out with this problem: I want to have a script that would change the nth field seperator in a line into something else. like a,d,4,2,97,8,9 into a,d,4,2,97/8/9 Thanks (2 Replies)
Discussion started by: onthetopo
2 Replies

9. Shell Programming and Scripting

regexp to print after a field seperator

Hi, How do i Print anything after a ':' Ex : file1: 1235131(rs32553) I want to print out "1235131(rs32553)" how do i do it. I know we can do this using awk but looking for the right syntax. Any help appreciated. Thanks, Ram (7 Replies)
Discussion started by: ramky79
7 Replies

10. UNIX for Advanced & Expert Users

find columns with whitespace as field seperator?

Hai I am using bash-2.03$ bash --version GNU bash, version 2.03.0(1)-release (sparc-sun-solaris) I am not able to use gawk command its showing command not found , why ? Eg: awk 'NR==1' fix.txt | gawk 'BEGIN { FIELDWIDTHS = "3 2" } { printf($1"|"$2); }'... (8 Replies)
Discussion started by: tkbharani
8 Replies
Login or Register to Ask a Question