Insert empty columns in a flat file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Insert empty columns in a flat file
# 1  
Old 03-11-2013
Insert empty columns in a flat file

Hi,

I have a tab delimited flat file, for example shown below
Code:
Name    Desg    Loc
a    b    c
d    e    f

I want to insert an empty column inbetween the column Desc and Loc, the result should be like shown below:

Code:
Name    LName    Desg    Loc
a                b      c
d                e      f

Can anybody please help.

Thanks

Last edited by sampoorna; 03-11-2013 at 04:15 AM.. Reason: column pos correction
# 2  
Old 03-11-2013
Code:
$ awk  -v OFS="\t" ' {NR==1?$2="LName\t"$2:$2="\t"$2 } 1 ' file
Name    LName   Desg    Loc
a               b       c
d               e       f

# 3  
Old 03-11-2013
thanks, can u plz explain the command
# 4  
Old 03-11-2013
OFS="\t" Set Output Field Separator to tab

NR==1?$2="LName\t"$2:$2="\t"$2 If it is first line(NR==1) then append "LName\t" to second field else append tab("\t") to second field

1 means true and print the current row
# 5  
Old 03-11-2013
thank you very much.. Smilie
# 6  
Old 03-11-2013
Code:
perl -pe 'if ($_ =~ "^Name")
{ s/^(\w+)(.*)/$1\tLname$2/g}
else
{s/^(\w+)(.*)/$1\t$2/g;}
' test

# 7  
Old 03-11-2013
Here is another possible way:
Code:
sed "s!\t!\tLName\t!" input.txt > temp1.x
sed "2,$ s!\tLName!\t!" temp1.x > output.txt

On one line:
Code:
sed "s!\t!\tLName\t!; 2,$ s!\tLName!\t!" input.txt

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Insert a value between two empty delimiter in the file.

Would like to insert value between two empty delimiter and at the very last too if empty. $ cat customerleft.tbl 300|Customer#000000300|I0fJfo60DRqQ|7|17-165-193-5964|8084.92|\N|p fluffily among the slyly express grouches. furiously express instruct||||||||||||||||||||||||\N... (3 Replies)
Discussion started by: Mannu2525
3 Replies

2. Shell Programming and Scripting

Insert empty columns inside a pipe delimited file

Hi All , I have pipe delimiter file with 11 columns . I need to insert 4 empty columns after column 10 . and After 11 column I need to insert a column which is having the same value for all the rows . My file 1|2|3|4|5|6|7|8|9|10|11 New file ... (11 Replies)
Discussion started by: Hypesslearner
11 Replies

3. UNIX for Dummies Questions & Answers

Insert row into empty file...how?

Greetings: I generate an empty flat file just fine when there's no data returned from my process, as the customer wants one always (using the 1st line of the below script). However, they also want at least the column names in this flat file (row 1, the only row to be in the emply file). I'm... (7 Replies)
Discussion started by: Benrosa
7 Replies

4. Shell Programming and Scripting

insert text into empty file

I have an awk script to extract data from several files and create output in the following format as a csv file: xxxx 01/04/12 0001 0 When data is present, I have a file. When no data is available in the input files, I would still like to create a file that looks like this: xxxx... (1 Reply)
Discussion started by: banjo25
1 Replies

5. Shell Programming and Scripting

Read flat file upto certain number of columns

Hello Guys Please help me with the below issue I want to read a flat file source upto certain number of columns Say my flat file has 30 columns but I want to read upto 25 columns only How come the above issue can be addressed? Thanks a lot!!!! (1 Reply)
Discussion started by: Pratik4891
1 Replies

6. UNIX for Dummies Questions & Answers

Audit Flat File - # of Columns / Rows

We receive a file which usually has 40 to 50 million rows. I want to set up an audit process by which everytime we receive a file we audit it for # of rows and total number of columns. So if the # of rows is around 1 million on a particular day, I want to raise a flag or send an email....and if... (3 Replies)
Discussion started by: priya33184
3 Replies

7. Shell Programming and Scripting

Sed insert text at first line of empty file

I can't seem to get sed to allow me to insert text in the first line of an empty file. I have a file.txt that is a 0 byte file. I want sed to insert " fooBar" onto the first line. I've tried a few options and nothing seems to work. They work just fine if there's text in the file tho. Help? (4 Replies)
Discussion started by: DC Slick
4 Replies

8. UNIX for Advanced & Expert Users

Insert Delimiter at fixed locations in a flat file

Hi Can somebody help me with solution for this PLEASE? I have a flat file and need to insert delimiters at fixed positions in all the lines so that I can easily convert into EXCEL with columns defined as per their width. For Example Here is the file { kkjhdhal sdfewss sdtereetyw... (7 Replies)
Discussion started by: jd_mca
7 Replies

9. Shell Programming and Scripting

How to insert at a particular position in flat file

Hi All, I have a flat file with ~ as de-limiter (e.g: aaa~ba a~caa~0~d~e) What I want is check if the 4th character is 0 and replace it with say 4. So now it becomes : aaa~ba a~caa~4~d~e. I have to do this for the whole file, but the delimiter position remains the same, not the... (10 Replies)
Discussion started by: akdwivedi
10 Replies

10. Shell Programming and Scripting

Help with Data Positioning from Columns in a flat file.

Hi All, I have used this forum many times to solve my many scripting problems. This time, I would like to seek some answers to a problem that I've been head scratching quite a bit on. My Example: I am converting a 2000-byte file into a 300-byte file this file has no delimiters and hardly any... (3 Replies)
Discussion started by: oott1
3 Replies
Login or Register to Ask a Question