Adding ' in particular fields


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Adding ' in particular fields
# 1  
Old 01-06-2017
Adding ' in particular fields

I have a file with 4 columns

a|b|c|d

I need to add single quotes around field 2 and 3

I need it to be like

a|'b'|'c'|d
# 2  
Old 01-06-2017
Any attempts / ideas / thoughts from your side, possibly fertilized by a search in these fora?
# 3  
Old 01-06-2017
Code:
sed 's.|.'|.g' file

but this adding to the last field as well


Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 01-06-2017 at 12:39 PM.. Reason: Added CODE tags.
# 4  
Old 01-06-2017
I'm afraid that wouldn't run at all as there's a ' missing...
awk OK? Try
Code:
awk -F\| -vSQ="'" '{$2 = SQ $2 SQ; $3 = SQ $3 SQ} 1' OFS=\| file
a|'b'|'c'|d

# 5  
Old 01-06-2017
Quote:
Originally Posted by dsravanam
I have a file with 4 columns
a|b|c|d
I need to add single quotes around field 2 and 3
I need it to be like
a|'b'|'c'|d
Hello dsravanam,

In case you have more than 4 fields then you could try following also(I haven't tested it though).
Code:
awk -F"|" -vs1="'" '{for(i=2;i<NF;i++){$i= s1 $i s1}} 1' OFS="|" Input_file

Thanks,
R. Singh
# 6  
Old 01-06-2017
sed:
Code:
sed "s/[^|]\{1,\}/'&'/2; s/[^|]\{1,\}/'&'/3" file

or
Code:
sed "s/|\([^|]*\)|\([^|]*\)|/|'\1'|'\2'|/" file

GNU sed:
Code:
sed -r "s/[^|]+/'&'/2; s/[^|]+/'&'/3" file


Last edited by Scrutinizer; 01-06-2017 at 01:39 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Is there a UNIX command that can compare fields of files with differing number of fields?

Hi, Below are the sample files. x.txt is from an Excel file that is a list of users from Windows and y.txt is a list of database account. $ head -500 x.txt y.txt ==> x.txt <== TEST01 APP_USER_PROFILE USER03 APP_USER_PROFILE TEST02 APP_USER_EXP_PROFILE TEST04 APP_USER_PROFILE USER01 ... (3 Replies)
Discussion started by: newbie_01
3 Replies

2. Shell Programming and Scripting

Awk: adding fields after matching $1

Dear AWK-experts! I did get stuck in the task of combining files after matching fields, so I'm still awkward with learning AWK. There are 2 files: one containing 3 columns with ID, coding status, and score for long noncoding RNAs: file1 (1.txt) (>5000 lines) ... (12 Replies)
Discussion started by: kben
12 Replies

3. Shell Programming and Scripting

Adding Fields to the file

Hi All, I get a file on weekly basis from client. I need to write a script which make sure the file should have 20 columns after the first column. If not then the script should add the remaining columns and default them to space(except for 2nd and 3rd). and at the same time the script should... (25 Replies)
Discussion started by: Arun Mishra
25 Replies

4. Shell Programming and Scripting

Adding fields to a file

Hi All, I have a file(Pipe Delimited) where i need to add a blank field before the last field and a blank field after the last field. Please help. I have provided below the sample input records and desired output. Sample Input: A0010000|Abilene TX A 1|A0010957|Dallas TX|A0010998|West|US... (5 Replies)
Discussion started by: Arun Mishra
5 Replies

5. Shell Programming and Scripting

Adding fields to file

Hi All, I have a file(Pipe Delimited) where i need to add a blank field before the last field and a blank field after the last field. Please help. I have provided below the sample input records and desired output. Code: Sample Input: A0010000|Abilene TX A 1|A0010957|Dallas... (0 Replies)
Discussion started by: Arun Mishra
0 Replies

6. Homework & Coursework Questions

regarding adding fields to DSR protocol in ns2.34

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: want to add field to route cache and packet of DSR routing protocol in ns2.34, add field, DSR package in ns2.34... (2 Replies)
Discussion started by: khubalkar
2 Replies

7. Programming

regarding adding fields to DSR protocol in ns2.34

hi i am student doing project in ns2.34. i hav to add field in route cache and packet of DSR routing protocol. which files hv to be changed...pl help me (1 Reply)
Discussion started by: khubalkar
1 Replies

8. Shell Programming and Scripting

Adding field to file and moving the last 2 fields

I have a file with 32 fields each separated by ‘|”. I need to add a file date exactly in the format “ "20100120" “ as the 32nd field moving the existing 32nd field to 33. so the field I added should be 32nd and the 33rd field is the last field before I added the file date. I know we can... (8 Replies)
Discussion started by: dsravan
8 Replies

9. Shell Programming and Scripting

Adding new fields to an existing layout

Hi Everybody, I have an layout file like below f1 1 char 10, f2 11 char 2, f3 13 char 1, lineend 14 char 1 Their I need to add a new field which would be like f5 char 3, f6 char 2 The o/p should be f1 1 char 10, f2 11 char 2, f3 13 char 1, f5 14 char 3, f6 17 char 2 (3 Replies)
Discussion started by: mr_manii
3 Replies

10. Shell Programming and Scripting

awk sed cut? to rearrange random number of fields into 3 fields

I'm working on formatting some attendance data to meet a vendors requirements to upload to their system. With some help on the forums here, I have the data close. But they've since changed what they want. The vendor wants me to submit three fields to them. Field 1 is the studentid field,... (4 Replies)
Discussion started by: axo959
4 Replies
Login or Register to Ask a Question