Computing.Net > Forums > Unix > substitute & retain field seprator

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

substitute & retain field seprator

Reply to Message Icon

Name: lakb200
Date: June 5, 2004 at 20:09:14 Pacific
OS: unix
CPU/Ram: intel pentium
Comment:

please help:

Fields in a record seperated with different number of spaces
abc dfdsfu dfsfymsa fafda

OUTPUT
abc dfdsfu dfsf sa fafda

Must reproduce the same file with all the spaces. The only change for the record is subsituting two particular positions with two blank spaces (dfsfymsa should change to
dfsf sa) How to read and out put this new file

lakb200



Sponsored Link
Ads by Google

Response Number 1
Name: Wolfbone
Date: June 6, 2004 at 02:08:36 Pacific
Reply:

cut -c 1-15,8- --output-delimiter=' ' file

There should be two spaces between the quotes.


0

Response Number 2
Name: lakb200
Date: June 6, 2004 at 06:57:28 Pacific
Reply:

I tried using it

cut -f1-112,115- -d' ' test.data

expecting to read as is from 1 to 112 and from 115 till end.

But output i got was the same as the input.

I need to read 1-112 spaces in 113 and 114 and read 115 till end.

Thanks


lakb200


0

Response Number 3
Name: Wolfbone
Date: June 6, 2004 at 07:30:39 Pacific
Reply:

Well that's not very surprising! Why did you change to using fields instead of characters and the input delimiter instead of the output delimiter? The obvious numeric modifications to the expression I gave should work.


0

Response Number 4
Name: lakb200
Date: June 6, 2004 at 07:43:29 Pacific
Reply:

I get this when I run it

cut -c 1-15,8- -d=' ' t.data
cut: invalid delimiter


is -d different from output delimiter.

Actually when I put the record in here the field sperators got defaulted to one space. But my records has each field seperated by different number of spaces. Am I doing anything wrong ? Does your command also substitue with spaces in those two positions ?

Thanks for all your help

lakb200


0

Response Number 5
Name: Wolfbone
Date: June 6, 2004 at 08:45:37 Pacific
Reply:

Wufgelazabarak!

There! - Now I have lifted the evil curse that was making it impossible for you to follow my instructions.

cut -c 1-112,115- --output-delimiter=' ' t.data

Don't forget that there must be two spaces between the single quotes.


0

Related Posts

See More



Response Number 6
Name: Jim Boothe
Date: June 7, 2004 at 08:28:00 Pacific
Reply:

The problem with the output-delimiter option is that it is effective only when using -f (field list) and not for -c (column specs).  The man page does not make that clear, but "info cut" indicates that it is for -f. Likewise, the -d input delimiter is also only for -f field lists.

With your variable number of spaces, you cannot use -f, so you cannot use the output-delimiter option.  The following solution sets the input field separator to null so that each whole line is read as a single field (the default would have been multiple fields separated by white space), and the echo incorporates two spaces inside the double-quotes.

IFS=
while read line
do
f1=$(echo $line | cut -c1-112)
f2=$(echo $line | cut -c115-)
echo "$f1  $f2"
done < filein > fileout

Or, an awk solution:

awk '{print \
  substr($0,1,112) "  " \
  substr($0,115)}' filein > fileout


0

Response Number 7
Name: Wolfbone
Date: June 7, 2004 at 09:29:22 Pacific
Reply:

Okay - my mistake - it appears cut was improved on January 9th 2003 but I didn't remember it had that limitation before.


0

Response Number 8
Name: Jim Boothe
Date: June 7, 2004 at 14:55:52 Pacific
Reply:

Never-the-less, I am always learning something new from you guys, and this time it was Wufgelazabarak!


0

Response Number 9
Name: lakb200
Date: June 7, 2004 at 17:53:59 Pacific
Reply:

Thanks so much.

I did resolve it so:
first_time=1
exec < $DATA_HOME/$AMEXFILE
while read -r line
do
head -1 $DATA_HOME/$AMEXFILE | awk '{print substr($0,1,112) " "
substr($0,115,3
28)}' > formatted_amex1205.txt
awk '{print substr($0,1,112) " " substr($0,115,328)}' >>
formatte
d_amex1205.txt
done


But as you can see I have a redundant processing for the first line with the head command. Some how my loop process all records except the first record. Any idea.


lakb200


0

Response Number 10
Name: Jim Boothe
Date: June 8, 2004 at 07:33:03 Pacific
Reply:

Wufgelazabarak!

I will explain the problems with your code in a subsequent reply, but first, just for the sake of experiment, please do me a favor ...

Copy/paste my first solution exactly as it is, except to change input and outfile filenames.  Change nothing else - just the file names - and see if the output is what you want.  Then copy/paste my second solution exactly as is, change only the file names, and see if that output is what you want.

Let us know the results.  Thanks.

In a short while, I will explain your code (whose behaviour is consistent with RedHat linux but not SuSE linux).


0

Response Number 11
Name: Jim Boothe
Date: June 8, 2004 at 08:57:11 Pacific
Reply:

Your while statement reads its first line (from the defaulted stdin that was declared by the exec statement) into the $line variable (which your code does not utilize).  That first while loop iteration burns (eats) the first line of your input (and does nothing with it).

Not to worry, because your head -1 infile | awk goes and gets that first line again (rather than utilize the copy sitting in $line), and sends it to the output file.

This is immediately followed by another awk statement that does not specify an input file, which means that it, also, will read the standard input that was declared by your exec statement.  On HP-UX and SuSE linux, it appears that awk continues to read the remainder of the stdin that has already been opened by the while loop (and the first line having already been eaten). Therefore (and stranger things have been known to happen), this code actually produces the desired output on HP-UX and SuSE linux.

However, on RedHat linux (and probably others), that second awk appears to open its own second copy of the (defaulted) stdin, so it processes the entire input file (including the first line).  So, your duplication is due to the head -1 infile | awk outputting record 1, then the second awk outputs the entire file again, including another record 1.

Note that all of this is happening on the first iteration of the while loop. That second awk statement consumes the entire input file.

Decide and visualize how you want to process a file.  Commands that process a file will retrieve all of the lines on their own, so they don't need to be inside a loop.  Or instead, you can use a "while read" loop to read and process one line at a time.  In that case, you will
read each input record into a variable (read line) or maybe read into several variables (read word1 word2 word3), then you can test or manipulate those variables and maybe output them with echo or print. Inside a while loop that is reading file1, you typically will not use commands that also read file1.


0

Response Number 12
Name: lakb200
Date: June 8, 2004 at 12:12:01 Pacific
Reply:

Thanks so much to identify the mystery. Jim the code that you gave does work. I appreciate all the insight you gave us.

lakb200


0

Response Number 13
Name: Dlonra
Date: June 9, 2004 at 17:40:37 Pacific
Reply:

karabazalegfuW! what am i missing?:
"abc dfdsfu dfsfymsa fafda

OUTPUT
abc dfdsfu dfsf sa fafda

... only change is ... subsituting dfsf sa for dfsfymsa"

sed 's/dfsfymsa/dfsf sa/' Origfile>NewFile


0

Response Number 14
Name: Jim Boothe
Date: June 10, 2004 at 11:18:48 Pacific
Reply:

That was just a sample line.

The code needs to change columns 113-114 to spaces in each line, regardless of content.


0

Sponsored Link
Ads by Google
Reply to Message Icon

multiple renaming files &... E-mail attachments for ba...



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: substitute & retain field seprator

Passing a field separator literally www.computing.net/answers/unix/passing-a-field-separator-literally/4656.html

awk/nawk field assignment scripting www.computing.net/answers/unix/awknawk-field-assignment-scripting/5911.html

modify Field no. 3 www.computing.net/answers/unix/modify-field-no-3/7182.html