Can sed be used to insert data at specific column?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Can sed be used to insert data at specific column?
# 1  
Old 03-21-2010
Question 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 timeout 1831

So inserting at a beginning of a line is easy, that would be:

cat inputfile | sed 's/^/timein /g' > outputfile

it's inserting the "timeout" that throwing me. Any help would be appreciated.
# 2  
Old 03-21-2010
Code:
awk '{print "timein",$1,"timeout", $2}' urfile

# 3  
Old 03-22-2010
Sticking w sed AND the assumption of two columns separated by space(s):

Code:
sed -e "s/ +/timeout/g" -e "s/^/timein/g" filename



---------- Post updated at 00:38 ---------- Previous update was at 00:38 ----------

Sticking w sed AND the assumption of two columns separated by space(s):

Code:
sed -e "s/ +/timeout/g" -e "s/^/timein/g" filename

# 4  
Old 03-22-2010
Thanks guys!SmilieSmilie
# 5  
Old 03-22-2010
Quote:
Originally Posted by curleb
Sticking w sed AND the assumption of two columns separated by space(s):

Code:
sed -e "s/ +/timeout/g" -e "s/^/timein/g" filename

---------- Post updated at 00:38 ---------- Previous update was at 00:38 ----------

Sticking w sed AND the assumption of two columns separated by space(s):

Code:
sed -e "s/ +/timeout/g" -e "s/^/timein/g" filename

We can combine as

Code:
 
sed 's/\([0-9]*\) \([0-9]*\)/Timein \1 Timeout \2/g' < file

# 6  
Old 03-22-2010
Code:
sed 's/^\(.[^ \t]*\)[ \t]*\(.[^ \t]*\)/timein \1 timeout \2/' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to insert data into black column( Secound Column ) in excel (.XLSX) file using shell script?

Source Code of the original script is down below please run the script and try to solve this problem this is my data and I want it column wise 2019-03-20 13:00:00:000 2019-03-20 15:00:00:000 1 Operating System LAB 0 1 1 1 1 1 1 1 1 1 0 1 (5 Replies)
Discussion started by: Shubham1182
5 Replies

2. Shell Programming and Scripting

Insert data in first column(if blank) from previous line first column

Dear Team I need to insert field(which is need to taken from previous line's first field) in first column if its blank. I had tried using sed but not find the way. Detail input and output file as below. Kindly help for same. INPUT: SCGR SC DEV DEV1 NUMDEV DCP ... (7 Replies)
Discussion started by: jaydeep_sadaria
7 Replies

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

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

sed - insert text if column empty

Hi, I want to insert the text 'Unknown' in 2 specific columns in a csv file (actually | separated) if the column is blank. Its always the same columns. I have tried using sed: sed "s/||/|Unknown|/g" but there are occasion where other fields are blank and they need to be left blank. This... (4 Replies)
Discussion started by: ksexton
4 Replies

6. Shell Programming and Scripting

Replace column that matches specific pattern, with column data from another file

Can anyone please help with this? I have 2 files as given below. If 2nd column of file1 has pattern foo1@a, find the matching 1st column in file2 & replace 2nd column of file1 with file2's value. file1 abc_1 foo1@a .... abc_1 soo2@a ... def_2 soo2@a .... def_2 foo1@a ........ (7 Replies)
Discussion started by: prashali
7 Replies

7. Shell Programming and Scripting

Insert and shifting data at column

Hi all, i have data like this joe : 1 :a bob : 2 :b sue : 3 :c foo : 4 :d at column 2 i want to insert TOP to the top column and at column 3 i want to insert BOTTOM to the bottom column. and the result will... (12 Replies)
Discussion started by: psychop13
12 Replies

8. Shell Programming and Scripting

insert data into specific lines of a CSV

So I work in a 1 to 1 laptop deployment and sometimes we need to mass order parts. The vendor will send us a text file and we have to manually input serial numbers. Well I have a full blown web based inventory system which I can pull serial number reports from. I then have to input the part... (4 Replies)
Discussion started by: tlarkin
4 Replies

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

10. Shell Programming and Scripting

How do I insert into a specific colunm using sed

I am trying to edit my /etc/shadow file globally to make each line change from: rrrtest:BBBuhiD49QA:12881:::::: rtest1:.GHXbqSCYU:12935:::::: To rrrtest:BBBuhiD49QA:12881::90:90::: rtest1:.GHXbqSCYU:12935::90:90::: I only want to change those columns. Any help or guidance would be... (7 Replies)
Discussion started by: tercex11
7 Replies
Login or Register to Ask a Question