search a line and insert string into specific at position


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting search a line and insert string into specific at position
# 1  
Old 03-20-2009
search a line and insert string into specific at position

Hi, guys. I have one question:

How can I search for a line with certain string in it and then insert a string into this line?
For example:

There is a file called shadow, the contents of it are below:

**************************
...
yuanz:VIRADxMsadfDF/Q:0:0:50:7:::
sherrys:ASdfe#$DfdadfFAd:0:0:50:7:::
brownj:ADdfTRMnD$%3dfa:0:0:50:7:::
...
**************************

A variable called $user, another variable called $string. I want to search the line which has $user in it, and add $string at the beginning of the second column. If $user is brownj, and $string is STRING_, then the line will be:

brownj:STRING_ADdfTRMnD$%3dfa:0:0:50:7:::

I use the following code, but it doesn't work

sed 's/^$user:/$user:$string/' shadow

Can anybody help me with this?

Thank you very much for your time in advance

-Keyang
# 2  
Old 03-21-2009
it is advisable not to change your shadow file like that. (however its still possible). change the password using commands such as passwd, usermod ...
# 3  
Old 03-21-2009
hi, ghostdog74.

I am not changing the password in this way, I want to suspend the user, I add a string to the encrypted password, so the user is not able to login. I am new to Linux, so I do not really know whether or not this is a good practice. And as a practice, I could not user command like "passwd" and "usermod". Still, thanks for your reply.

@general:
Does anybody can help me with this question?

Thank you very much for your time in advance

-Keyang
# 4  
Old 03-21-2009
please check the passwd and usermod man page. there are options to "suspend" user.
# 5  
Old 03-21-2009
awk again Smilie
Code:
awk '{FS=OFS=":"}/brownj/{$2="STRING_"$2}1' shadow

# 6  
Old 03-21-2009
Quote:
Originally Posted by ghostdog74
please check the passwd and usermod man page. there are options to "suspend" user.
Hi, ghostdog74.

I am not supposed to use commands like suspend and passwd for this exercise. Smilie

Still, thanks for the reply. Smilie

-Keyang
# 7  
Old 03-21-2009
Quote:
Originally Posted by danmero
awk again Smilie
Code:
awk '{FS=OFS=":"}/brownj/{$2="STRING_"$2}1' shadow

Hi, Dan.

Thanks for answering again! I will try it out.

But can you tell me what is wrong with my code? if I don't use variables in my code, it works well. For example:

sed 's/^brownj:/brownj:STRING_/' shadow

It will find brownj and replace it with brownj:STRING_.

Thanks for your time in advance

-Keyang
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search for a string at a particular position and replace with blank based on position

Hi, I have a file with multiple lines(fixed width dat file). I want to search for '02' in the positions 45-46 and if available, in that lines, I need to replace value in position 359 with blank. As I am new to unix, I am not able to figure out how to do this. Can you please help me to achieve... (9 Replies)
Discussion started by: Pradhikshan
9 Replies

2. Shell Programming and Scripting

Search a string in a text file and add another string at the particular position of a line

I am having a text file which is having more than 200 lines. EX: 001010122 12000 BIB 12000 11200 1200003 001010122 2000 AND 12000 11200 1200003 001010122 12000 KVB 12000 11200 1200003 In the above file i want to search for string KVB and add/replace... (1 Reply)
Discussion started by: suryanarayana
1 Replies

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

4. Shell Programming and Scripting

Search string position in a line

WeeksD1|$26.52|$26.52|03/02/2013|12.48|D1|1.09|0|0|0|0|0|.95|0|0|0|0|0|0|0|0|0|0|0|0|0|0||$0.00|$0.00||0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0||$0.00|$0.00||0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0||$0.00|$0.00||0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|$0.00|$0.00|$0.00|$0.00|$0.00|$0.00|$... (3 Replies)
Discussion started by: gavemani
3 Replies

5. Shell Programming and Scripting

substitute a string on a specific position for specific lines

I woud like to substitue a string on a specific position for specific lines I've got a file and I would like to change a specific string from "TOCHANGE" to "ABCABCAB" For every line (except 1,2, 3 and the last one) , I need to check between the 9th and the 16th digits. For the 3rd line, I... (7 Replies)
Discussion started by: BSF
7 Replies

6. Shell Programming and Scripting

Search in specific position and print the whole line

I have two files abc.dat and sant.dat (Big file 60k rows) for every line's 1,4 of abc.dat need to seach if this is present on 28,4 of sant.dat every line. if its present the output needs to go to bde.dat Example: contents abc.dat aaaa bbbb cccc dddd contents sant.dat this is... (4 Replies)
Discussion started by: ssantoshss
4 Replies

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

8. Shell Programming and Scripting

KSH - search for string and insert line above

Hi,, Please excuse my lack of knowledge here. I think I am tring to do something fairly advanced yet am a bit of a beginner..... I want to use a korn shell (as is the norm with others in my company) to write a script to search a file for a string and insert some text above that line. Sudo... (5 Replies)
Discussion started by: HugoDarley
5 Replies

9. Shell Programming and Scripting

Print lines with search string at specific position

Hi Folks, I have a file with all fields defined by byte position, but any field can be empty so I cannot print lines based on a search of specific columns. I need to print all lines of this file where the string of two characters at byte position 100-101 contains the number 27. Any ideas? ... (4 Replies)
Discussion started by: HealthyGuy
4 Replies

10. Shell Programming and Scripting

how to insert line break + string in vi (search & replace )

Hello all i have big test file that has allot of structure text something like this : <foo1 *.html> <blah action> somthing 1 somthing 2 </blah> </foo1 > now i will like to insert 2 more lines of text below the <blah action> so it will be like : <foo1... (1 Reply)
Discussion started by: umen
1 Replies
Login or Register to Ask a Question