Insert strings/values between text


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Insert strings/values between text
# 1  
Old 10-16-2012
Insert strings/values between text

Hello Guru,
I'm trying to insert a value between 2 fields (between last and second last field) But end up the script actually replacing the value in the second last field. What should i put to fix the problem?

Input File:
Code:
apple,mango,grape,lemon

Expected output:
Code:
apple,mango,grape,0,lemon

Code:
awk -F, 'BEGIN{OFS=","}{$(NF-1)="0";print}' input

my output (grape is missing)
Code:
apple,mango,0,lemon

Thanks
# 2  
Old 10-16-2012
Try changing your code to:
Code:
awk -F, 'BEGIN{OFS=","}{$(NF-1)=$(NF-1) OFS "0";print}'

or
Code:
awk -F, 'BEGIN{OFS=","}{$NF="0" OFS $NF;print}'

This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 10-16-2012
Quote:
Originally Posted by Don Cragun
Try changing your code to:
Code:
awk -F, 'BEGIN{OFS=","}{$(NF-1)=$(NF-1) OFS "0";print}'

or
Code:
awk -F, 'BEGIN{OFS=","}{$NF="0" OFS $NF;print}'

Hai Don,
Thanks it works perfectlySmilie. But can you explain a bit your second method. I just don't get how the code insert the value 0 in the middle.

Thanks again. Smilie
# 4  
Old 10-16-2012
Please try the below.

Code:
 awk -F, '{$4="1,"$4}1' OFS=,  test.txt

This User Gave Thanks to bmk For This Post:
# 5  
Old 10-16-2012
Quote:
Originally Posted by null7
Hai Don,
Thanks it works perfectlySmilie. But can you explain a bit your second method. I just don't get how the code insert the value 0 in the middle.

Thanks again. Smilie
Both methods are the same; the 1st one adds ,0 to the end of the next to the last field; the 2nd one adds 0, to the start of the last field. Putting two strings next to each other when assigning a string value to a variable concatenates those strings. Both use OFS (the awk output field separator) instead of a literal comma in case you change from a comma separated value file format to use some other field separator at some point in the future.
This User Gave Thanks to Don Cragun For This Post:
# 6  
Old 10-19-2012
Understood. Thank you guys for your helped. Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Insert values into template

I have 2 files. Template file: SELECT NAME = "" DEATILS Input file: SERVER1 06/05/2016 10:00:00 06/05/2016 05:08:59 SERVER2 06/04/2016 09:50:00 06/05/2016 01:03:59 SERVER3 06/06/2016 11:26:00 06/06/2016 10:31:55 I want to generate the output file that look like this: ... (6 Replies)
Discussion started by: vinus
6 Replies

2. Shell Programming and Scripting

Insert missing values

Hi, please help with this, I need to insert missing values into a matrix for a regression analysis. I have made up an example. The first three columns are variables with levels and the next 3 are values, the 4th column missing values should be replaced by 0s, and 5th and 6th column missing... (3 Replies)
Discussion started by: ritakadm
3 Replies

3. Shell Programming and Scripting

Read record from the text file contain multiple separated values & assign those values to variables

I have a file containing multiple values, some of them are pipe separated which are to be read as separate values and some of them are single value all are these need to store in variables. I need to read this file which is an input to my script Config.txt file name, first path, second... (7 Replies)
Discussion started by: ketanraut
7 Replies

4. Shell Programming and Scripting

Insert values

HI Guys, I have a data in a file in the below format 45783 23457 23556 54584 Now i want to convert this data into the below format reader='45783' or reader='23457' or reader='23556' or reader='54584' Please help how to convert as i am applying loop but not able to get the data... (6 Replies)
Discussion started by: jaituteja
6 Replies

5. Shell Programming and Scripting

how to Insert values in multiple lines(records) within a pipe delimited text file in specific cols

this is Korn shell unix. The scenario is I have a pipe delimited text file which needs to be customized. say for example,I have a pipe delimited text file with 15 columns(| delimited) and 200 rows. currently the 11th and 12th column has null values for all the records(there are other null columns... (4 Replies)
Discussion started by: vasan2815
4 Replies

6. Shell Programming and Scripting

Korn shell program to parse CSV text file and insert values into Oracle database

Enclosed is comma separated text file. I need to write a korn shell program that will parse the text file and insert the values into Oracle database. I need to write the korn shell program on Red Hat Enterprise Linux server. Oracle database is 10g. (15 Replies)
Discussion started by: shellguy
15 Replies

7. Shell Programming and Scripting

Need to insert new text and change existing text in a file using SED

Hi all, I need to insert new text and change existing text in a file. For that I used the below line in the command line and got the expected output. sed '$a\ hi... ' shell > shell1 But I face problem when using the same in script. It is throwing the error as, sed: command garbled:... (4 Replies)
Discussion started by: iamgeethuj
4 Replies

8. Shell Programming and Scripting

How to insert some constant text at beginig of each line within a text file.

Dear Folks :), I am new to UNIX scripting and I do not know how can I insert some text in the first column of a UNIX text file at command promtp. I can do this in vi editor by using this command :g/^/s//BBB_ e,g I have a file named as Test.dat and it containins below text: michal... (4 Replies)
Discussion started by: Muhammad Afzal
4 Replies

9. Shell Programming and Scripting

Insert two strings at the beginning and at the end of each line of a file

Hi, excuse me for my poor english. My problem is that: I have a File i want to add to each line of that file two strings: one at the beginning of the line, one at the ending. string1="abcd" string2="efgh" i want $string1 content $string2 for each line. Is that possible? (3 Replies)
Discussion started by: Linux-fueled
3 Replies

10. Shell Programming and Scripting

How to insert strings at certain position

Hi, I need to insert strings "0000 00" at the each line within the file. The postion is 37 to 42. ex. name1 name2 0000 00 nam name 0000 00 The "0000 00" in two lines should be lined up. I don't know why it's not lined up when I posted it. Can anyone help? (14 Replies)
Discussion started by: whatisthis
14 Replies
Login or Register to Ask a Question