Awk Concat

Hi All

this may be somewhere in internet , but couldnt find the it.

i have file as

abc01
2010-07-01 12:45:24
2010-07-01 12:54:35
abc02
2010-07-01 12:59:24
2010-07-01 01:05:13
abc03
.
.
.

the output using awk should look like this

abc01|2010-07-01 12:45:24|2010-07-01 12:54:35
abc02|2010-07-01 12:59:24|2010-07-01 01:05:13
abc03|...........................................
.................................

similarly i have some 25 abc values

thanks for any help

sed -n "N;N;s/\n/|/g;p" file

---------- Post updated at 02:02 PM ---------- Previous update was at 01:04 PM ----------

paste -d"|" - - - < file

Or

awk ' { ORS=( NR % 3 == 0 ? "\n" : "|" ) } 1 ' file
1 Like
# cat infile
abc01
2010-07-01 12:45:24
2010-07-01 12:54:35
abc02
2010-07-01 12:59:24
2010-07-01 01:05:13
abc03
.....
.....
# sed -e '{N;N;s/\n/|/g;}'  infile
abc01|2010-07-01 12:45:24|2010-07-01 12:54:35
abc02|2010-07-01 12:59:24|2010-07-01 01:05:13
abc03|.....|.....
# ./justdoit infile
abc01|2010-07-01 12:45:24|2010-07-01 12:54:35
abc02|2010-07-01 12:59:24|2010-07-01 01:05:13
abc03|.....|.....
 
# justdoit
i=1
lin=""
check=3
while read -r LINE
 do
  lin="$lin|$LINE"
  if [ $i -eq $check ] ; then
  echo "$lin" | sed 's/^.//g'
  lin=""
  let check=$check+3
  fi
  let i=$i+1
done <$1

If the lines between abc... is not always two lines, you can try this code:

awk 'BEGIN{OFS="|"} /^abc/ {printf RS $0 OFS ;next} {printf $0 OFS}' urfile |sed 's/|$//'