I have a file with the following content:
a-123-345-232
a-23343-4545-545
a-67676-45454-8787
a-129-8912-9824
b-564-78678-2322
b-5454-76767-8899
b-85554-124-152-29
c-34534-654543-323
(... and so on, actually these are pretty huge records)
Now, I want that the file should not be broken in to multiple files but instead, a delimiter, say "#"should be inserted after all lines with beginning with "a" end again after the lines starting with "b" end and so on...
for example,
a-123-345-232
a-23343-4545-545
a-67676-45454-8787
a-129-8912-9824
#
b-564-78678-2322
b-5454-76767-8899
b-85554-124-152-29
#
c-34534-654543-323
Thanks....
hergp
August 16, 2013, 5:56am
2
Try this awk-script
BEGIN {
last=" ";
}
{
if (last !~ /^a/ && $1 ~ /^a/) print "#";
last = $1;
print;
}
awk -f script.awk <input >output
mjf
August 16, 2013, 6:13am
3
Here's another way using awk:
awk -F- '{if (NR==1) {first=$1}} {if (first==$1) print $0; else {print "#"; print $0; first=$1}}' file.txt
1 Like
Thank you so much hergp and mjf :). It worked....
hergp
August 16, 2013, 6:20am
5
Your solution is correct, mjf, thank you.
My solution prints the hash only at the change from not-a to a. I misread the OP. Sorry.
hergp:
Your solution is correct, mjf, thank you.
My solution prints the hash only at the change from not-a to a. I misread the OP. Sorry.
Though, the solution of mjf worked and your's was printing a "#", once before the output, else verbatim the input, I appreciate your effort.
rdrtx1
August 16, 2013, 8:59am
7
try also:
awk '! a[substr($0,1,1)]++ && NR>1 { print "#"} 1' infile
Jotne
August 16, 2013, 9:44am
8
Does not work if first letter is repeated down in the file like
a-
a-
b-
b-
c-
c-
a-
b-
Yet another alternative assuming a grouped file:
awk -F- 'p!=$1{if(NR>1)print "#"; p=$1}1' file