How do I insert into a specific colunm using sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How do I insert into a specific colunm using sed
# 1  
Old 06-03-2005
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 appreciated.

John
# 2  
Old 06-03-2005
nawk -F: -v OFS=':' '{$5=$6=90;print}' /etc/shadow

Last edited by vgersh99; 06-03-2005 at 03:11 PM..
# 3  
Old 06-04-2005
Or if you really want to use sed, you must throw yourself a metacharacter party Smilie
Code:
$ sed 's/^\([^:]*:[^:]*:[^:]*:[^:]*:\)[^:]*:[^:]*\(:.*\)$/\190:90\2/' /etc/shadow

Cheers
ZB
# 4  
Old 06-04-2005
slim till dead Smilie
Code:
sed 's/\(.*\)::\(.*:.*:\)/\190:90:\2/'

# 5  
Old 06-04-2005
When dealing with the shadow file I would be much happier to use ZBs version than the last post, since his method does not depend on the content of the fields, merely that the format of the file is valid. The last post depends on the content of the fields also.

Personally I would use nawk since the command is much simple, and when dealing with essential system files I am firmly of the opinion that the simpler it can be done the better, especially for once-off changes.
# 6  
Old 06-04-2005
Quote:
Originally Posted by r2007
slim till dead Smilie
Code:
sed 's/\(.*\)::\(.*:.*:\)/\190:90:\2/'

Slim, but not robust Smilie

What happens if one of the lines in shadow already has a value in that particular field?

rrrtest:BBBuhiD49QA:12881::9090:90:::

Oooh dear Smilie

But with the code I gave....

rrrtest:BBBuhiD49QA:12881::90:90:::

Smilie

Cheers
ZB
# 7  
Old 06-04-2005
ZB you beat me to the punch, I could see problems with that but hadn't had the chance to run a test yet.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Get records which contend same value in specific colunm

Gents. Please a help. I have this input I will like to count how many times is repeated the record which have always the values 14 and 98 only. 1000 1 1000 1 1001 1 1001 1 1002 98 1002 98 ... (9 Replies)
Discussion started by: jiam912
9 Replies

2. Shell Programming and Scripting

Insert charactera in 1st position of specific lines using vi editor or sed command

Dear all, i am having text file like below surya rama ranga laxman rajesh reddy i want add string (OK) before a text from line 3 to 5 the result will be surya rama OK ranga OK laxman OK rajesh reddy (1 Reply)
Discussion started by: suryanarayana
1 Replies

3. Solaris

Insert a file at specific line

Hi, Anyone can help me in Solaris command on how to insert a file at specific line. I want file1.sql content to be inserted on file2.sh after "recover database using backup controlfile until cancel". # file1.sql /archivelogs/927_822338133.arc /archivelogs/671_822338107.arc... (3 Replies)
Discussion started by: fspalero
3 Replies

4. Shell Programming and Scripting

Insert | in specific position

Hi , I have a file which has line similar to below 13123324234234234234234234234234234 3454546456dfhgfhgh454645654asdasfsdsddfgdgdfg 345345345mnmnbmnb346mnb4565464564564645645 Not for each line for specific position I need to insert some '|' Positions are fixed. Like 3,5,9,11 So the... (5 Replies)
Discussion started by: Anupam_Halder
5 Replies

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

6. Shell Programming and Scripting

Replace specific field on specific line sed or awk

I'm trying to update a text file via sed/awk, after a lot of searching I still can't find a code snippet that I can get to work. Brief overview: I have user input a line to a variable, I then find a specific value in this line 10th field in this case. After asking for new input and doing some... (14 Replies)
Discussion started by: crownedzero
14 Replies

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

8. Shell Programming and Scripting

Insert character in a specific position of a file

Hi, I need to add Pipe (|) at 5th and 18th position of all records a file. How can I do this? I tried to add it at 5th position using the below code. It didnt work. Please help!!! awk '{substr($0,5,1) ~ /|/}{print}' $input_file > $temp_file (1 Reply)
Discussion started by: gpaulose
1 Replies

9. Shell Programming and Scripting

using sed to replace a specific string on a specific line number using variables

using sed to replace a specific string on a specific line number using variables this is where i am at grep -v WARNING output | grep -v spawn | grep -v Passphrase | grep -v Authentication | grep -v '/sbin/tfadmin netguard -C'| grep -v 'NETWORK>' >> output.clean grep -n Destination... (2 Replies)
Discussion started by: todd.cutting
2 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