Insert space or pattern between columns in a data file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Insert space or pattern between columns in a data file
# 8  
Old 05-27-2013
Quote:
Originally Posted by spacebar
Code:
$ awk '{ print $1","$2":"$3 }' f1
123,abc:456
xyz,789:efg

But it then removes the spaces that I already have, e.g.

Code:
awk -v OFS='       ' '{$1=$1};1' f1 | awk '{ print $1","$2":"$3 }'

removes the space I created before the pipeline !

I do not want to change the spaces between columns if I already have. Recreating the spaces artificially by another pipeline may not be desired.
# 9  
Old 05-27-2013
Code:
sed '
s/ \( *\)/:\1/2
s/ \( *\)/,\1/1
' f1

# 10  
Old 05-27-2013
Quote:
Originally Posted by hbar
removes the space I created before the pipeline !
Correct, Those are two different examples, the first one just shows how to use the Output Field Separator(OFS) to specify exactly what goes between each field, the second one just shows how to output the fields specifying a character or characters to place between each field without using Output Field Separator(OFS), so when you piped the first one into the second one the spaces you created between each column in the first one are dropped and it outputs the ',' and ':' between the columns.
Using the second awk example(awk '{ print $1","$2":"$3 }' f1) you can specify something different between each column( space(s), comma, colon, slashe(s), etc.
# 11  
Old 05-28-2013
Quote:
Originally Posted by spacebar
Correct, Those are two different examples, the first one just shows how to use the Output Field Separator(OFS) to specify exactly what goes between each field, the second one just shows how to output the fields specifying a character or characters to place between each field without using Output Field Separator(OFS), so when you piped the first one into the second one the spaces you created between each column in the first one are dropped and it outputs the ',' and ':' between the columns.
Using the second awk example(awk '{ print $1","$2":"$3 }' f1) you can specify something different between each column( space(s), comma, colon, slashe(s), etc.
I think I can create my own spaces and insert comma, colon etc between them. But it may be difficult to exploit the spacing that I already have between columns.
# 12  
Old 05-28-2013
Why did my previous post not meet your requirement?
The \( *\) matched the extra space characters and \1 inserted them back.
\( *\) matches all the space characters i.e. the entire space.
The /2 addresses the 2. space while the /1 addresses the 1. space.
Two more examples:
Code:
sed '
s/\(  *\)/:\1/2
s/\(  *\)/,\1/1
' f1

Code:
sed '
s/\(  *\)/  \1/2
s/\(  *\)/  \1/1
' f1

# 13  
Old 05-28-2013
Quote:
Originally Posted by MadeInGermany
Why did my previous post not meet your requirement?
The \( *\) matched the extra space characters and \1 inserted them back.
\( *\) matches all the space characters i.e. the entire space.
The /2 addresses the 2. space while the /1 addresses the 1. space.
Two more examples:
Code:
sed '
s/\(  *\)/:\1/2
s/\(  *\)/,\1/1
' f1

Code:
sed '
s/\(  *\)/  \1/2
s/\(  *\)/  \1/1
' f1

The colon and comma sit just after column 2 and column 1. Can I place them in the middle of the spaces?

For example, if I have 4 spaces, a comma will sit on the 3rd space and there will be a gap of 5 spaces between the columns (including the comma in the middle).

For odd number of spaces, we can make a choice, such as make even by adding an extra space and then put the comma in the middle.

We can exploit this idea to generate a table while I'll be inserting '|' in the middle of two columns.

I was expecting some 'awk' commands, but don't know whether that could be possible.

Thanks anyway.
# 14  
Old 05-28-2013
You can match and re-add a space character at the beginning:
Code:
sed '
s/ \( *\)/ :\1/2
s/ \( *\)/ ,\1/1
' f1

Attempts to find the middle with \( *\)\1 are problematic;
the following only works with all spaces being even number of space characters:
Code:
sed '
s/\(  *\)\1/\1:\1/2
s/\(  *\)\1/\1,\1/1
' f1

---------- Post updated at 01:59 PM ---------- Previous update was at 01:52 PM ----------

Hmm, I am surprised, it seems to work even with odd spaces.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Insert content of file before the first occurrence of a line starts with a pattern in another file

Hi all, I'm new to scripting.. facing some problems while inserting content of a file into another file... I want to insert content of a file (file2) into file1, before first occurrence of "line starts with pattern" in file1 file1 ====== working on linux its unix world working on... (14 Replies)
Discussion started by: Jagadeesh Kumar
14 Replies

2. Shell Programming and Scripting

Insert data based on pattern

if it is finding some data based on pattern 'test' then insert else if has no data based on the pattern 'test' then exit successfully cat file | grep test > file2 (3 Replies)
Discussion started by: jagu
3 Replies

3. Shell Programming and Scripting

Need to Insert three extra columns in csv file

Hello Experts, I got a requirement i have a input file which am getting from different source,Now i want to add extra 3 columns to this file like BASE,ACTUAL and DATE. Input File Looks like QUAL CHGE TYP LAW COM1 COM2 A 1 X SED HO ASE B 3 Z CDE SE ... (5 Replies)
Discussion started by: ahmed.vaghar
5 Replies

4. Shell Programming and Scripting

Delete and insert columns in a tab delimited file

Hi all , I have a file having 12 columns tab delimited . I need to read this file and remove the column 3 and column 4 and insert a word in column 3 as "AVIALABLE " Is there a way to do this . I am trying like below Thanks DJ cat $FILENAME|awk -F"\t" '{ print $1 "\t... (3 Replies)
Discussion started by: Hypesslearner
3 Replies

5. 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

6. Shell Programming and Scripting

Insert space in specific column among many columns

Hello, I have some problem in inserting the space for the pairs of columns. I have the input file : I used this code below in replacing it using space in specific column (replace space in each two columns) sed -e "s/,/ /2" -e "s/,/ /3" inputfile Output showed : However, I have many... (3 Replies)
Discussion started by: awil
3 Replies

7. Shell Programming and Scripting

Insert empty columns in a flat file

Hi, I have a tab delimited flat file, for example shown below Name Desg Loc a b c d e fI want to insert an empty column inbetween the column Desc and Loc, the result should be like shown below: Name LName Desg Loc a b c d e ... (6 Replies)
Discussion started by: sampoorna
6 Replies

8. Shell Programming and Scripting

help - sed - insert space between string of form XxxAxxBcx, without replacing the pattern

If the string is of the pattern XxxXyzAbc... The expected out put from sed has to be Xxx Xyz Abc ... eg: if the string is QcfEfQfs, then the expected output is Qcf Ef Efs. If i try to substitute the pattern with space then the sed will replace the character or pattern with space,... (1 Reply)
Discussion started by: frozensmilz
1 Replies

9. Shell Programming and Scripting

insert text into another file after matching pattern

i am not sure what i should be using but would like a simple command that is able to insert a certain block of text that i define or from another text file into a xml file after a certain match is done for e.g insert the text </servlet-mapping> <!-- beechac added - for epic post-->... (3 Replies)
Discussion started by: cookie23patel
3 Replies

10. Shell Programming and Scripting

search a pattern and if pattern found insert new pattern at the begining

I am trying to do some thing like this .. In a file , if pattern found insert new pattern at the begining of the line containing the pattern. example: in a file I have this. gtrow0unit1/gctunit_crrownorth_stage5_outnet_feedthru_pin if i find feedthru_pin want to insert !! at the... (7 Replies)
Discussion started by: pitagi
7 Replies
Login or Register to Ask a Question