Inserting a field without disturbing field separator on other fields
Hi All,
I have the input as below:
I need to add a value at 19th field without disturbing the field separator between the fields.
The field separator in my input is space. and each field contains 6 positions in it except the first field.
Now the value i need to add at 19th field is below:
My output should look like below:
I have tried the below codes:
I have got the same output from the above two codes as below:
Here in the above output, if we observe,the bb value got inserted at 19th field in the input file. But the spaces got removed from the fields which contains a combination of blank space and values.
For ex:- see field2. field2 = 2.891 but after inserting bb value the leading blank space at field2 got removed. Likewise blankspaces got removed in other fields as well.
Thanks for the reply. I will try on this and will let you know.
Regards,
am24
---------- Post updated 05-12-16 at 02:52 AM ---------- Previous update was 05-11-16 at 03:52 AM ----------
Hi Rudi,
I have tried the below code.
with the above code, i can keep the 19th field value as 2.979 and the field width also adjusted properly. But the problem is the original value of 19th field in my input 76.265 is getting removed. Al other field values are coming correctly.
I just need to insert 2.979 in 19th filed but i should not remove any other field values. How can i overcome this ?
Thanks for the reply. I have modified the loop as you suggested. In the output , 2.979 placed in 20th field and 76.265 placed in 19th field.
So i just modified as below:
Now the value 2.979 is placed in 19th field and 76.265 in 20th field. and all other fields placed correctly.
Also i have question that , if 2.979 value is stored in one variable, then can i assign it to T[19] ?
Reason for this is, the 2.979 is not constant value, i am taking the value from some other file. So if i pull out the value from the file and store it in one variable then can i assign the variable value to array element ?
I have tried something on this but did not get the proper result.
You want to insert that value, i.e. it will be $19, $19 will become $20, etc.? Then you need to shove every single value one index back. Like for (i=CNF; i>18; i--) $(i+1) = $i.
IF there's NO empty fields in your file, you could extend the 19th field like $19 = 2.979 " " $19, and then split $0 into T for formatted output.
EDIT: Of course you can use a variable for the value to be inserted: $19 = var " " $19! But - it needs to be an awk variable, NOT a shell variable.
Hello All,
I have a file, but I want to separate the file at a particular record with comma"," in the line
Input file
APPLE6SSAMSUNGS5PRICEPERPIECEDOLLAR600EACH010020340URX581949695US
to
Output file
APPLE6S,SAMSUNGS5,PRICEPERPIECE,DOLLAR600EACH,010020340URX581949695,US
This is for... (11 Replies)
Hi Experts ,
file :
- How to construct the awk filed separator so that $1, $2 $3 , can be assigned to the each "" range.
I am trying : awk -F"]" '{print $1}'
but it is printing the entire file. Not first field.
The desired output needed for first field... (9 Replies)
Hi !
input:
111|222|333|aaa|bbb|ccc
999|888|777|nnn|kkk
444|666|555|eee|ttt|ooo|ppp
With awk, I am trying to change the FS "|" to "; " only from the 4th field until the end (the number of fields vary between records).
In order to get:
111|222|333|aaa; bbb; ccc
999|888|777|nnn; kkk... (1 Reply)
I have a '|' delimited file.
My file looks like below
23|nationalhoilday|feb12||||||||||||||california|northdistrict||
In the same way, each record has 164 fields. I have to insert one more field after the 85th field.
Expected output... (3 Replies)
I am trying to echo all fields except for the last field.
I want to include the field seperator, but it is removed.
echo "a;s;v;g" | awk -F ";" '{$(NF--)=""; print}'
a s v
I want an output like this:
a;s;v; (3 Replies)
Hi,
I have a flat file with fields separated by a X'1F'
i have to fetch 4th field from second line.
please help me how to achieve it.
I tried with below command and its not working.
cut -f4 -d`echo -e '\x1f'` filename.txt
I am using SunOS.
Thanks in advance. (2 Replies)
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)
Hi
I need to check if field separator I am using in awk statement is " : ", for example:
TIME=12:59
HOUR=`echo "$TIME" | awk '{FS=":"; print $1}'`
MINUTES=`echo "$TIME" | awk '{FS=":"; print $2}'`
Is there a way to check within the above awk statement ?
Thanks for help -A (2 Replies)
Hi,
I have a file that has data in it that says
00:01:48.233 1212
00:01:56.233 345
00:09:01.221 5678
00:12:23.321 93444
The file has more line than this but i just wanted to put in a snippet to ask how I would get the highest number with time stamp into another file. So from the above... (2 Replies)
Hi,
I have the following data in the format as shown (note: there are more than 1 blank spaces between each field and the spaces are not uniform, meaning there can be one blank space between field1 and field2 and 3 spaces between field3 and field4, in this example, # are the spaces in between... (19 Replies)