Visit Our UNIX and Linux User Community

How to append a character to the last but one field on a specific line?

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to append a character to the last but one field on a specific line?
# 1  
Old 02-02-2010
How to append a character to the last but one field on a specific line?

Hi Guys,

I have a file like this:

aaa  b c d e f 
fsss  g h i  k l
qqq   r t h n

I want:

aaa  b c d e  f 
fsss  g h i  k  l
qqq   r t h  ,  n 
ggg   p t e d u 
qqq   i o s ,  k

So I want to insert a comma at the last but one location of every line that matches "qqq".

I tried to use the gsub command but could not get the second from last character to be inserted in awk.

Thanks in advance.
# 2  
Old 02-02-2010
awk ' /qqq/ { sub(" [^ ]*$"," ,&") } 1 ' file

Last edited by anbu23; 02-02-2010 at 09:05 PM..
# 3  
Old 02-03-2010
Ok that script works when the row with the qqq has only one field missing. How can I modify the script if there are one or more fields missing for every row with qqq?

For example if I have: (i have used "a" instead of "qqq")

a b c d e f 
a f g u  i
a r d  u
a n

So I want:

a b c d e f 
a f g u  , i
a r d ,  , u
a , ,  ,  , n

Basically I want the last character on a line that matches "a" to line up with the column that has the maximum fields.
Thanks again.

Last edited by npatwardhan; 02-03-2010 at 08:40 PM..

Previous Thread | Next Thread
Test Your Knowledge in Computers #710
Difficulty: Easy
Chrome DevTools. when you want to inspect the styles or attributes of a DOM node , right-click the element and select Inspect.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Count specific character of a file in each line and delete this character in a specific position

I will appreciate if you help me here in this script in Solaris Enviroment. Scenario: i have 2 files : 1) /tmp/TRANSACTIONS_DAILY_20180730.txt: 201807300000000004 201807300000000005 201807300000000006 201807300000000007 201807300000000008 2)... (10 Replies)
Discussion started by: teokon90
10 Replies

2. UNIX for Beginners Questions & Answers

Append each line based upon the character size

I have a huge file which contains multiple lines. It need to check whether character length is not more than 255 each line. If its not then it should remove the character up to column. I have described in the output below. If its more than that the next line should start with call but if the... (1 Reply)
Discussion started by: JoshvaPeter
1 Replies

3. Shell Programming and Scripting

File Parsing based on a character in a specific field

Hi All, I'm having a hard time finding a starting point for my issue. I have a 30k line file (fspsec.txt) that I would like to parse into smaller files based on any character existing in field 1. ACCOUNTANT LEVEL 1 (ACCT.ACCOUNTANT) OPERATORS: DOEJO (418) TOOLS: Branch Maintenance ... (2 Replies)
Discussion started by: aahlrich
2 Replies

4. Shell Programming and Scripting

How to print with awk specific field different from specific character?

Hello, i need help with awk. I have this file: cat number DirB port 67 er_enc_out 0 er_bad_os 0 DirB port 71 er_enc_out 56 er_bad_os 0 DirB port 74 er_enc_out 0 er_bad_os 0 DirB port 75 ... (4 Replies)
Discussion started by: elilmal
4 Replies

5. Shell Programming and Scripting

Replace specific field on specific line sed or awk

I'm trying to update a text file via sed/awk, after a lot of searching I still can't find a code snippet that I can get to work. Brief overview: I have user input a line to a variable, I then find a specific value in this line 10th field in this case. After asking for new input and doing some... (14 Replies)
Discussion started by: crownedzero
14 Replies

6. Shell Programming and Scripting

Using awk to read a specific line and a specific field on that line.

Say the input was as follows: Brat 20 x 1000 32rf Pour 15 p 1621 05pr Dart 10 z 1111 22xx My program prompts for an input, what I want is to use the input to locate a specific field. Like if I type in, "Pou" then it would return "Pour" and just "Pour" I currently have this line but it is... (6 Replies)
Discussion started by: Bungkai
6 Replies

7. HP-UX

How to remove new line character and append new line character in a file?

Hi Experts, I have data coming in 4 columns and there are new line characters \n in between the data. I need to remove the new line characters in the middle of the row and keep the \n character at the end of the line. File is comma (,) seperated. Eg: ID,Client ,SNo,Rank 37,Airtel \n... (8 Replies)
Discussion started by: sasikari
8 Replies

8. Shell Programming and Scripting

awk append to one line if first field is the same

Hi all, How would I append the second field each time to one line if the first field is the same for example I have this data: 10430,187976 10430,251588 10430,262904 10430,275008 10430,279892 10430,275008 10430,303740 10430,318136 10430,336988 10430,350324 10430,373648 And I... (4 Replies)
Discussion started by: borderblaster
4 Replies

9. Shell Programming and Scripting

append a character at end of each line of a file

Hi, i want to append a character '|' at end of each line of a file abc.txt. for example if the file abc.txt conatins: a|b|c 1|2|33 w|2|11 i want result file xyz.txt a|b|c| 1|2|33| w|2|11| I know this is simple but sumhow i am not able to reach end of line. its urgent, thanks for... (4 Replies)
Discussion started by: muaz
4 Replies

10. Shell Programming and Scripting

Using SED to append character to each line

Hey - my first post here, and I'm a total SED newb. I've looked around for previous help on this, but have so far been unsuccessful. I have a program (AMStracker for OS X) that outputs data in the terminal. Output is in this form: . . . 3 0 -75 3 0 -76 3 0 -77 ... (4 Replies)
Discussion started by: c0nn0r
4 Replies

Featured Tech Videos