I'm fairly new to scripting and I have a problem that I am having difficulty solving.
What I'd like to do is run an awk script to adjust the string in the first field depending on the string in another field. This is best explained with an example:
Here is my script:
For each line, the above script reads the string in the first and sixth fields. Should the first field contain 01, 02, 31 or 36 and the sixth field contain 55, then the number in the first field is substituted and the line printed. Otherwise, the line is printed unaltered.
Here is my input file:
and here is the actual output:
For reasons that I do not understand, the sub commands have stripped the white space between the fields and replaced it with a single space, despite using a print $0 statement. Is there another command that I can use instead of sub that will preserve the white space?
The output that I am looking for is:
where the correct number of spaces are preserved between the fields.
I'm sorry if this problem has been addressed before - I did a brief search, but I couldn't find a solution. Please feel free to direct me to a solution if this has been addressed before. Also, if possible, I'd prefer to keep the script in a file that can be called using awk -f scriptname.
Thank you so much Franklin52 and in2nix4life!I can't believe that the solution was so straight forward.
I prefer to have a script file so that I can send it to others to use, although the one liner is also useful. I'm guessing that I could make the one liner even longer by getting it to print the lines that do not have replaced strings like the script in my opening post.
Hi ,
Can any one help me out how to remove space from below line
select file_name from dba_data_files where tablespace_name='SYSTEM ----space---- ';
i want as :
select file_name from dba_data_files where tablespace_name='SYSTEM';
my code i use
file=/u01/script/latest/tbs.temp
while... (14 Replies)
hi guys
how can i add spacein file name with sed if strings have no space around dash
input
19-20
( 18-19 )
ABC-EFG
output after add white space
19 - 20
(18 - 19 )
ABC - EFG
thx in advance (2 Replies)
Hi
How to remove white space from this input:|blue | 1|
|green| 4|
|black| 2|
I like to search for green and get 4not 4
How to modify this to work correct:awk -F"|" '/green/ {print $3} (7 Replies)
Hi,
Consider the data (FS = |):
1| England |end
2| New Zealand |end
3|Australia|end
4| Some Made Up Country |end
5| West Indies|end
I want the output to be (i.e. without the leading and trailing white space from $2)
England
New Zealand
Australia
Some Made Up Country
West... (4 Replies)
I have an AWK script that uses multiple delimiters in the FS variable.
FS="+"
My awk script takes a file name such as this:
12345_smith_bubba_12345_20120215_4_0.pdf and parses it out based on the under score. Each parsed field then has some code for data validation etc.
This script has... (12 Replies)
our user creates a text file with a white space on the filename. this same file is transfered to unix via automation tool. i have a korn shell script that reads these files on a input directory and connects to oracle database to run the oracle procedures which will load the data from each of the... (2 Replies)
HI,
I need the help from the experts like I have created one file with text like:
Code:
a b c de f g hi j k l
So my question is that i have to write the script in which like in the first sentence it will take only one space after d and remove all the extra space in the end.I dont... (0 Replies)
Hi,
I have a large flat file from host without delimiter. I'm transforming this file to a csv file using statements like
# Row 03: Customer / field position 3059 +20
WOFABNAM=substr( $0, 3059, 20 );
and deleting the trailing whitespaces before and after with that
sub( /^ +/, "",... (4 Replies)
Hi,
What sed command (if sed is the right command) can remove ALL white space from my file. I have a csv, except I want to remove all white space between commas and characters.
My idea (without testing)
sed 's/ //g'
Is there a better way? (18 Replies)
Dear Members,
Suppose i have a variable test which stores a string as below:
test='John drives+++++++++a+++++car'
now i want to use sed on the above variable and replace + with a white space, so that i get
echo $test should give me
'john drives a car'
Between... (1 Reply)