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
# 1  
Old 05-27-2013
Insert space or pattern between columns in a data file

I have a data file where three data sets are written in three columns. Can I increase the space between the columns without reading them?


Also can I insert particular patterns, say comma between 1st and 2nd column and colon between 2nd and 3rd column?
# 2  
Old 05-27-2013
This is some simple examples:
Code:
$ cat f1
123 abc 456
xyz 789 efg

# Replace space between 1st and 2nd column with 3 spaces
$ sed 's# #   #' f1
123   abc 456
xyz   789 efg

# Replace space between all columns with 3 spaces
$ sed 's# #   #g' f1
123   abc   456
xyz   789   efg

# Replace space between 1st and 2nd column with a comma
$ sed 's# #,#' f1
123,abc 456
xyz,789 efg

# Replace space between all columns with a comma
$ sed 's# #,#g' f1
123,abc,456
xyz,789,efg

# 3  
Old 05-27-2013
Not unless the data from all three data sets is a constant number of bytes in each line in the file AND you are replacing a constant number of bytes with the same number of bytes that increases the space between them (such as by replacing a space character with a tab character). Under these circumstances you could write a C or C++ program to just write replacement characters into fixed positions in the file. (Even then the OS would have to read the blocks containing the bytes that surround the bytes you're replacing; but your program wouldn't have to directly read the file.)

If you want to insert a comma or a colon AND increase the space between fields, you cannot do so without reading the data unless you'd be replacing a multi-byte character of length 2 or more with a single-byte comma or colon and another single-byte character (such as space or tab) and enough other single-byte characters to exactly fill the bytes occupied by the multi-byte character you'd be replacing.

If you're willing to read the file, there are thousands of ways to change field separators between fields if you'll give us an accurate description of your input file(s) including what the input field separators are and describe what field separators you want to replace them in your output.
# 4  
Old 05-27-2013
Quote:
Originally Posted by spacebar
This is some simple examples:
Code:
$ cat f1
123 abc 456
xyz 789 efg

# Replace space between 1st and 2nd column with 3 spaces
$ sed 's# #   #' f1
123   abc 456
xyz   789 efg

# Replace space between all columns with 3 spaces
$ sed 's# #   #g' f1
123   abc   456
xyz   789   efg

# Replace space between 1st and 2nd column with a comma
$ sed 's# #,#' f1
123,abc 456
xyz,789 efg

# Replace space between all columns with a comma
$ sed 's# #,#g' f1
123,abc,456
xyz,789,efg


I get only change in space between 1st and 2nd column. Could you explain what is happening?

I also believe that there must be a smart 'awk'-way to do these.
# 5  
Old 05-27-2013
Two awk examples:
Code:
# Separate columns with 3 spaces
$ awk -v OFS='   ' '{$1=$1};1' f1
123   abc   456
xyz   789   efg

# Separate columns with a comma
$ awk -v OFS=',' '{$1=$1};1' f1
123,abc,456
xyz,789,efg

# 6  
Old 05-27-2013
Quote:
Originally Posted by spacebar
Two awk examples:
Code:
# Separate columns with 3 spaces
$ awk -v OFS='   ' '{$1=$1};1' f1
123   abc   456
xyz   789   efg

# Separate columns with a comma
$ awk -v OFS=',' '{$1=$1};1' f1
123,abc,456
xyz,789,efg

What about my original second question:
comma between 1st and 2nd column, and colon between 2nd and 3rd column ?
# 7  
Old 05-27-2013
Quote:
Originally Posted by hbar
comma between 1st and 2nd column, and colon between 2nd and 3rd column ?
Code:
$ awk '{ print $1","$2":"$3 }' f1
123,abc:456
xyz,789:efg

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