Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
please help:
Fields in a record seperated with different number of spaces
abc dfdsfu dfsfymsa fafdaOUTPUT
abc dfdsfu dfsf sa fafdaMust 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 filelakb200

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

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.

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

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.

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 > fileoutOr, an awk solution:
awk '{print \
substr($0,1,112) " " \
substr($0,115)}' filein > fileout

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

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

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

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).

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.

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

karabazalegfuW! what am i missing?:
"abc dfdsfu dfsfymsa fafdaOUTPUT
abc dfdsfu dfsf sa fafda... only change is ... subsituting dfsf sa for dfsfymsa"
sed 's/dfsfymsa/dfsf sa/' Origfile>NewFile

That was just a sample line.
The code needs to change columns 113-114 to spaces in each line, regardless of content.

![]() |
multiple renaming files &...
|
E-mail attachments for ba...
|

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