Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Placing numbers 1

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Hi,

I've created a script that prints an output like:

@Participants: Tom
@ID: Tom
@Participants: Cathy, Peter
@ID: Cathy
@ID: Peter
@Participants: Bart, Karen, Lizzy
@ID: Bart
@ID: Karen
@ID: Lizzy

I want to add numbers before a line that matches @Participants so that my output becomes:

1. @Participants: Tom
@ID: Tom
2. @Participants: Cathy, Peter
@ID: Cathy
@ID: Peter
3. @Participants: Bart, Karen, Lizzy
@ID: Bart
@ID: Karen
@ID: Lizzy

Does anyone know how I should do this? Thanks!

Univ
 
Hi Univ,

You can prepend the string like this:

awk '{

if ( $0 ~ /Participants/ ) {
$1 = ++i". "$0
print
} else {
$1 = " "$0
print
}

}' infile > outfile

Hope this helps


flogrr
flogr@yahoo.com

 

If the poster wanted to sequentially number the "Participants" lines in the file, there is a bug in the proposed solution:

{

if ( $0 ~ /Participants/ ) {
$0 = ++i"."$0
print
} else {
$0 = " "$0
print
}
}

If the poster wanted to count the number of corresponding items for a given "Participants" line, the solution might be:

{

if ( $0 ~ /Participants/ ) {
$0 = split(substr($0, index($0, ":")+1), tmp, ",")"."$0
print
}
else {
$0 = " "$0
print
}
}

Input:

@Participants: Tom
@ID: Tom
@Participants: Bart, Karen, Lizzy
@ID: Bart
@ID: Karen
@ID: Lizzy
@Participants: Cathy, Peter
@ID: Cathy
@ID: Peter

Output:

1.@Participants: Tom
@ID: Tom
3.@Participants: Bart, Karen, Lizzy
@ID: Bart
@ID: Karen
@ID: Lizzy
2.@Participants: Cathy, Peter
@ID: Cathy
@ID: Peter


vlad
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top