Search a file for certain strings and add them to the end of certain lines


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Search a file for certain strings and add them to the end of certain lines
# 1  
Old 04-23-2015
Search a file for certain strings and add them to the end of certain lines

I have a log file which lists groups and users in the following format

Code:
GROUP1
user1
user2
user3
GROUP2
user4
user5
user6
GROUP3
user7
user8

I need to change the format to:

Code:
user1|GROUP1
user2|GROUP1
user3|GROUP1
user4|GROUP2
user5|GROUP2
user6|GROUP2
user7|GROUP3
user8|GROUP3

Does anybody know how I should approach this?

Thanks


Moderator's Comments:
Mod Comment Use code tags, thanks.

Last edited by zaxxon; 04-23-2015 at 10:31 AM..
# 2  
Old 04-23-2015
Please use code tags in future - I edited your post accordingly.

Code:
$ awk '/GROUP/ {g=$1; next} {print $1,g}' OFS=\| infile
user1|GROUP1
user2|GROUP1
user3|GROUP1
user4|GROUP2
user5|GROUP2
user6|GROUP2
user7|GROUP3
user8|GROUP3

# 3  
Old 04-23-2015
That worked great - thank you very much!
# 4  
Old 04-23-2015
Hello Angela_S,

Following may help you in same too.
Code:
awk '{if($1 ~ /^GROUP/){a=$1} else {print $0 "|" a}}'  Input_file

Output will be as follows.
Code:
user1|GROUP1
user2|GROUP1
user3|GROUP1
user4|GROUP2
user5|GROUP2
user6|GROUP2
user7|GROUP3
user8|GROUP3


Thanks,
R. Singh
 
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 Keyword in file and replace another keyword or add at the end of line

Hi I want to implement something like this: if( keyword1 exists) then check if(keyword2 exists in the same line) then replace keyword 2 with New_Keyword else Add New_Keyword at the end of line end if eg: Check for Keyword JUNGLE and add/replace... (7 Replies)
Discussion started by: dashing201
7 Replies

2. UNIX for Dummies Questions & Answers

Add strings from one file at the end of specific lines in text file

Hello All, this is my first post so I don't know if I am doing this right. I would like to append entries from a series of strings (contained in a text file) consecutively at the end of specifically labeled lines in another file. As an example: - the file that contains the values to be... (3 Replies)
Discussion started by: gus74
3 Replies

3. Shell Programming and Scripting

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

Dear All 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... (5 Replies)
Discussion started by: suryanarayana
5 Replies

4. Shell Programming and Scripting

awk to search similar strings and add their values

Hi, I have a text file with the following content: monday,20 tuesday,10 wednesday,29 monday,10 friday,12 wednesday,14 monday,15 thursday,34 i want the following output: monday,45 tuesday,10 wednesday,43 friday,12 (3 Replies)
Discussion started by: prashu_g
3 Replies

5. Shell Programming and Scripting

search and replace, when found, delete multiple lines, add new set of lines?

hey guys, I tried searching but most 'search and replace' questions are related to one liners. Say I have a file to be replaced that has the following: $ cat testing.txt TESTING AAA BBB CCC DDD EEE FFF GGG HHH ENDTESTING This is the input file: (3 Replies)
Discussion started by: DeuceLee
3 Replies

6. Programming

search a file between two begin and end strings in c

Can any one help me out with following problem... I want to search in a file which has two strings repeat each time(like start and end) i want to search between these two string in C programming. please help me with the solution. thanks in advance. (8 Replies)
Discussion started by: uday.sena.m
8 Replies

7. Shell Programming and Scripting

Delete lines in file containing duplicate strings, keeping longer strings

The question is not as simple as the title... I have a file, it looks like this <string name="string1">RZ-LED</string> <string name="string2">2.0</string> <string name="string2">Version 2.0</string> <string name="string3">BP</string> I would like to check for duplicate entries of... (11 Replies)
Discussion started by: raidzero
11 Replies

8. Shell Programming and Scripting

awk how to search strings within a file from two different lines

Hi, i would really appreciate any help anyone can give with the following info. Thanks in advance. I need to run a search on a file that contains thousands of trades, each trade is added into the file in blocks of 25 lines. i know the search has to take place between a time stamp specified... (4 Replies)
Discussion started by: sp3arsy
4 Replies

9. Shell Programming and Scripting

different take on common ?: search for two strings and remove lines between them

Thank you for assisting, I've got a partial solution just needs a tweak. Hulk-BASH$ cat somefile.txt oh there is some stuff here some more stuff here START_LABEL stuff I want more stuff I want END_LABEL other stuff here too and even more stuff here too Hulk-BASH$ Hulk-BASH$ sed... (8 Replies)
Discussion started by: laser
8 Replies

10. HP-UX

Add a column at the end of all the lines in a file

Hi Guys, :D I am very much new to UNIX. I dont have much basics of coding in UNIX, so please help me out of thi ssituation. I have a file say for ex: ABC.dtd and it contains "|" delimited data as test1|testing|test3|moving past1|runing|test4|going I need to add a column at the end... (6 Replies)
Discussion started by: ruthless
6 Replies
Login or Register to Ask a Question