Insert space in specific column among many columns


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Insert space in specific column among many columns
# 1  
Old 08-03-2013
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 :
Quote:
A,A,B,C,D,R
C,R,A,A,B,R
R,D,A,R,B,R
I used this code below in replacing it using space in specific column (replace space in each two columns)
Code:
sed -e "s/,/ /2" -e "s/,/ /3" inputfile

Output showed :
Quote:
A,A B,C D,R
C,R A,A B,R
R,D A,R B,R
However, I have many columns around 17,000 columns. I do not know how to use loop in this command. Could you please suggest me?

Thanks in advance Smilie
# 2  
Old 08-03-2013
sed have no loop command, use awk
Code:
awk -F, '{for (i=1;i<=NF;i+=2) printf "%s,%s ",$i,$(i+1);print ""}' inputfile
A,A B,C D,R
C,R A,A B,R
R,D A,R B,R

This User Gave Thanks to Jotne For This Post:
# 3  
Old 08-03-2013
Quote:
Originally Posted by Jotne
sed have no loop command, use awk
Code:
awk -F, '{for (i=1;i<=NF;i+=2) printf "%s,%s ",$i,$(i+1);print ""}' inputfile
A,A B,C D,R
C,R A,A B,R
R,D A,R B,R

Great. It is worked. Thank you very much Jotne.
# 4  
Old 08-03-2013
Quote:
Originally Posted by Jotne
sed have no loop command, use awk
You are mistaken. Explicit looping can be implemented with labels ( : ) and either or both of the branching (b) and testing (t) commands. However, a sed solution does not require any explicit looping, only a single substitution command:
Code:
sed 's/\([^,]*,[^,]*\),/\1 /g'

N.B. The sed solution never alters the number of fields in a record. That AWK solution will add a comma and an empty field if there are an odd number of fields (i.e. an even number of commas).

Regards,
Alister

Last edited by alister; 08-03-2013 at 04:11 PM..
This User Gave Thanks to alister For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Change some string in specific column with space

Hello All of Master Script , i need help to solve my problem before : mount /dev/rdsk/c1t69d0s6 /dev/rdsk/c1t69d0s6 /vol/cl123/PURGE1 ufs mount /dev/rdsk/c1t70d0s6 /dev/rdsk/c1t70d0s6 /vol/cl123/PURGE2 ufs expected : mount /dev/dsk/c1t69d0s6 /dev/rdsk/c1t69d0s6 /PURGE1 ufs mount ... (3 Replies)
Discussion started by: k0p0nkkk
3 Replies

2. Shell Programming and Scripting

Insert Columns before the last Column based on the Count of Delimiters

Hi, I have a requirement where in I need to insert delimiters before the last column of the total delimiters is less than a specified number. Say if the delimiters is less than 139, I need to insert 2 columns ( with blanks) before the last field awk -F 'Ç' '{ if (NF-1 < 139)} END { "Insert 2... (5 Replies)
Discussion started by: arunkesi
5 Replies

3. Shell Programming and Scripting

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? (13 Replies)
Discussion started by: hbar
13 Replies

4. Shell Programming and Scripting

Insert text into specific column

I have the following data: Dec 24 11:31:10 0000008b 9911662486 Answered Price SGD 0.003 PERIOD: 0 m 6 s Dec 24 11:21:42 00000086 9911662486 Answered Price SGD 0.001 PERIOD: 0 m 2 s Dec 20 15:34:28 00000004 9911662486 Answered Price SGD 0.007 PERIOD: 0 m 12 s Dec 20 18:42:30 0000017b... (6 Replies)
Discussion started by: alegnagrp
6 Replies

5. Shell Programming and Scripting

Selecting Specific Columns and Insert the delimiter TAB

Hi, I am writing a Perl Script for the below : I have a data file that consists of the header information which is 231 Lines and the footer information as 4 lines. The total number of line including the header and footer 1.2 Million with Pipe Delimited file. For example: Header Information:... (4 Replies)
Discussion started by: filter
4 Replies

6. UNIX for Dummies Questions & Answers

How do you view specific columns from a space delimited text file?

I have a space delimited text file with 1,000,000+ columns? I would only like to view specific ones (let's say through 1:10), how can I do that? Thanks! (3 Replies)
Discussion started by: evelibertine
3 Replies

7. Shell Programming and Scripting

remove white space from specific columns in text file

Hello i have a text file like this: 1 AB AC AD EE 2 WE TR YT WW 3 AS UY RF YT the file is bigger , but that's an example of the data what i want to do is to merge all columns together except the first one, it will become like this : 1 ABACADEE 2 WETRYTWW 3 ASUYRFYT (8 Replies)
Discussion started by: shelladdict
8 Replies

8. UNIX for Dummies Questions & Answers

How to insert alternative columns and sort text from first column to second?

Hi Everybody, I am just new to UNIX as well as to this forum. I have a text file with 10,000 coloumns and each coloumn contains values separated by space. I want to separate them into new coloumns..the file is something like this as ad af 1 A as ad af 1 D ... ... 1 and A are in one... (7 Replies)
Discussion started by: Unilearn
7 Replies

9. Shell Programming and Scripting

Can sed be used to insert data at specific column?

I'm trying to use sed to insert data at a specific column, let's say my data looks like this: 0553 1828 0552 1829 0550 1829 0549 1830 0548 1831 what I want is this: timein 0553 timeout 1828 timein 0552 timeout 1829 timein 0550 timeout 1829 timein 0549 timeout 1830 timein 0548... (5 Replies)
Discussion started by: mswartz
5 Replies

10. Shell Programming and Scripting

Insert a text from a specific row into a specific column using SED or AWK

Hi, I am having trouble converting a text file. I have been working for this whole day now, still i couldn't make it. Here is how the text file looks: _______________________________________________________ DEVICE STATUS INFORMATION FOR LOCATION 1: OPER STATES: Disabled E:Enabled ... (5 Replies)
Discussion started by: Issemael
5 Replies
Login or Register to Ask a Question