Insert sign every n character (awk)


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Insert sign every n character (awk)
# 1  
Old 04-23-2014
Insert sign every n character (awk)

Hi,

For example, I would like to insert a pipe every 4 characters for each second field (including after the last block).

input (coma separated):
Code:
line1,AAAABBBBCCCCDDDDEEEE
line2,FFFFGGGGHHHHIIIIJJJJ

output:
Code:
line1,AAAA|BBBB|CCCC|DDDD|EEEE|
line2,FFFF|GGGG|HHHH|IIII|JJJJ|


my attempt:
Code:
awk '
BEGIN{FS=OFS=","}

{a=split($2, b, "")
    for(i=1; i<=a; i+=5){
        print $1 OFS b[i]b[i+1]b[i+2]b[i+3]b[i+4] "|"
    }
}' input > output

# 2  
Old 04-24-2014
Code:
awk 'BEGIN{OFS=FS=","}{gsub(/..../,"&|",$2)}1'


Last edited by pilnet101; 04-24-2014 at 12:59 AM.. Reason: More accurate..
# 3  
Old 04-24-2014
If you might have uppercase letters before the comma, of if there might be characters that are not uppercase letters after the comma, you might find that this works (although pilnet101's script is much simpler if your input data is all like that shown in your sample input):
Code:
awk -F, '
{	printf("%s%s", $1, FS)
	for(i = 1; i < length($2); i += 4)
		printf("%s|", substr($2, i, 4))
	print ""
}' input

If you want to run this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk, /usr/xpg6/bin/awk, or nawk.

Note that using a null string as an ERE in split() will work in some implementations of awk, but produces syntax errors in other versions of awk.

Using print() instead of printf() in your loop prints a line for every 5 characters in the 2nd field instead of just adding a "|" after every 4 characters as requested.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Awk: greater than sign is working upside down

Hi, I noticed a weird behaviour with awk. input: A|B|1-100|blabla_35_40_blabla;blabla_53_60_blabla;blabla_90_110_blabla Objective: For each string separated by ';' in $4, if the first and second numbers are included in the interval in $3, then print "TRUE". Otherwise print "FALSE". In... (3 Replies)
Discussion started by: beca123456
3 Replies

2. Shell Programming and Scripting

Percentage sign causing awk problems

looks like awk gets confused when there's a % next to a number. command im running: awk -F" " '/phxnaz001b/ && /vol/ && NF { if (($NF >= 80) && ($NF < 83)) { print ; print ; w++ } else if ($NF >= 83) { print ; c++ } } END { printf("%d:OK %d:WARNING %d:CRITICAL\n", o, w, c) }' /tmp/test.log ... (3 Replies)
Discussion started by: SkySmart
3 Replies

3. Shell Programming and Scripting

Insert New Line Character

Hi, If my first character of a line starts with 2 then after 5th charecter newline character should be inserted. Input.txt: a1234567890 2222300007 bsdfsdf888999999 ssdfkjskfdjskfdjd 2899900000000099999999999999 28887777 999999999999999999 Output.txt: a1234567890 22223 00007... (8 Replies)
Discussion started by: unme
8 Replies

4. Shell Programming and Scripting

Insert a character before a line

I have a file and I can get the line with a specific pattern. I want to inset # on start of the line. file.text ==== aa bb cc bb hh kk kk ll yy dd aa kk rr tt aa I want to comment out the line with contain "aa" after running the script file.text ==== #aa bb cc bb hh kk kk ll... (7 Replies)
Discussion started by: Biplab
7 Replies

5. Shell Programming and Scripting

sed to insert a character

Hi all, I have a file like this Q8N302 21-84 Q8N157 15-45 Q99996 167-201 202-251 269-318 I want to insert a character or space if the line starts with a number and I used the command sed 's/^/#/' But in the output file, when it inserts this character, first digit in the number is... (2 Replies)
Discussion started by: kaav06
2 Replies

6. Shell Programming and Scripting

insert character in particular position.

I want to insert space in 7th position of all the lines usign vi editor or sed command Input file 12345689010 abcdefghijk . . Output file 123456 89010 abcdef ghijk . . (7 Replies)
Discussion started by: Jairaj
7 Replies

7. UNIX for Dummies Questions & Answers

Insert character in the line

Hi All, I have below type file. abc|asd|pqr|2|2|2 asc|qwe|scf|5|4|4 Pipe location and count is dynamic and coming from a variable. I want to change it to below files. (chnage the first pipe to 3 pipes) abc|||asd|pqr|2|2|2 asc|||qwe|scf|5|4|4 (chnage the second pipe to 4 pipes)... (1 Reply)
Discussion started by: swat
1 Replies

8. UNIX for Dummies Questions & Answers

How can I insert character to end of file?

Hello all How can I insert character to the end of text file without opening it in vi Just simple one liner, can it be done? Tnx (1 Reply)
Discussion started by: umen
1 Replies

9. Shell Programming and Scripting

Searching for + sign in a string using awk

Hi All, My query is: I have string say xyz+ how to determine that whether it ends with a + sign or not using awk command. (5 Replies)
Discussion started by: satyajit2512
5 Replies

10. Shell Programming and Scripting

awk insert character in the middle of a line

I'm trying to insert a single character at position 11 in everyline of a file. My input file looks like this: 456781 ~Y~12345 456782 ~N~12300 and I want my output to look like this: 45678~1 ~Y~12345 45678~2 ~N~12300 I tried the following awk code, but it's not working:... (3 Replies)
Discussion started by: mmarino
3 Replies
Login or Register to Ask a Question