Print String Every Specific Line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Print String Every Specific Line
# 1  
Old 02-04-2013
Print String Every Specific Line

Dear All,

I have input file like this,

001
059
079
996
758
079
069
059
079
...
...

Desired output:

AA 001
BB 059
CC 079
DD 996
AA 758
BB 079
CC 069
DD 059
AA 079
...
...

print different string (AA,BB,CC,DD) every multiples of four.....

Thanks Advance,


Attila
# 2  
Old 02-04-2013
Try something like:
Code:
awk 'BEGIN {
        p[0] = "DD"
        p[1] = "AA"
        p[2] = "BB"
        p[3] = "CC"
}
{       printf("%s %s\n", p[NR % 4], $0)}' input

As always, if you are using a Solaris/SunOS system, use /usr/xpg4/bin/awk or nawk instead of awk.
# 3  
Old 02-05-2013
Or sth like this..

Code:
 
awk 'BEGIN{split("DD AA BB CC",P)}
{print P[NR%4+1],$0}' file

# 4  
Old 02-05-2013
Quote:
Originally Posted by pamu
Or sth like this..

Code:
 
awk 'BEGIN{split("DD AA BB CC",P)}
{print P[NR%4+1],$0}' file

Not quite. In awk, + has higher precedence than % (so you're doing mod 5 rather than (mod 4) + 1). If you change the subscript in the 2nd line to (NR%4)+1, you'd get what you wanted.

-------------------------------------
Ouch... I apologize. I was looking at unary + not binary +. Pamu's code is correct as written.

Sorry,
Don
# 5  
Old 02-05-2013
you can use this also

Code:
  sed -e "1~4s/^/AA /g" -e "2~4s/^/BB /g" -e "3~4s/^/CC /g" -e "4~4s/^/DD /g" $file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to print string if tag is specific value

In the below awk I am trying to print expName only if another tag planExecuted is true. In addition to the expName I am also printing planShortID. For some reason the word experiment gets printed so I remove it with sed. I have attached the complete index.html as well as included a sample of it... (1 Reply)
Discussion started by: cmccabe
1 Replies

2. Shell Programming and Scripting

awk - how to print specific field if a string is matched

hi gurus, I would like to be able to use awk to process 1 file as such: abc 1 2 3 4 5 6 7 8 9 10 flags 1 2 4 flags 1 2 5 abc 2 3 4 5 6 7 8 9 10 11 flags 1 2 3 abc 4 5 6 7 8 9 6 7 78 89 flags 1 2 3 flags 1 2 4 flags 1 2 3 4 I would like to be able to print field 1 and 5 when the... (4 Replies)
Discussion started by: revaroo
4 Replies

3. UNIX for Dummies Questions & Answers

How to Detect Specific Pattern and Print the Specific String after It?

I'm still beginner and maybe someone can help me. I have this input: the great warrior a, b, c and what i want to know is, with awk, how can i detect the string with 'warrior' string on it and print the a, b, and c seperately, become like this : Warrior Type a b c Im still very... (3 Replies)
Discussion started by: radynaraya
3 Replies

4. Shell Programming and Scripting

break the string and print it in a new line after a specific word

Hi Gurus I am new to this forum.. I am using HP Unix OS. I have one single string in input file as shown below Abc123 | cde | fgh | ghik| lmno | Abc456 |one |two |three | four | Abc789 | five | Six | seven | eight | Abc098 | ........ I want to achive the result in a output file as shown... (3 Replies)
Discussion started by: kannansr621
3 Replies

5. Shell Programming and Scripting

Need awk help to print specific columns with as string in a header

awk experts, I have a big file of 4000 columns with header. Would like to print the columns with string value of "Commands" in header. File has "," separator. This file is on ESX host with Bash. Thanks, Arv (21 Replies)
Discussion started by: arv_cds
21 Replies

6. Shell Programming and Scripting

search-word-print-specific-string

Hi, Our input xml looks like: <doc> <str name="account_id">1111</str> <str name="prd_id">DHEP155EK</str> </doc> - <doc> <str name="account_id">6666</str> <str name="prd_id">394531662</str> </doc> - <doc> <str name="account_id">6666</str> <str... (1 Reply)
Discussion started by: Jassz
1 Replies

7. Shell Programming and Scripting

Select a specific part of the string and print it

Hi all, I have a string that looks like: #!/bin/sh options="arguments: --user=alpha --group=beta --prefix=/usr/share --proxy-path=/proxy --proxy-tmp=/tmp --conf-path=/etc" My goal is to transform the string into an array, then for each key, if it starts with "--proxy" to print the string... (2 Replies)
Discussion started by: TECK
2 Replies

8. Shell Programming and Scripting

search a word and print specific string using awk

Hi, I have list of directory paths in a variable and i want to delete those dirs and if dir does not exist then search that string and get the correct path from xml file after that delete the correct directory. i tried to use grep and it prints the entire line from the search.once i get the entire... (7 Replies)
Discussion started by: dragon.1431
7 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

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
Login or Register to Ask a Question