Awk Concat


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Awk Concat
# 1  
Old 07-28-2010
Awk Concat

Hi All

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


i have file as
Code:
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
Code:
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

Last edited by Franklin52; 07-29-2010 at 03:48 AM.. Reason: Please use code tags!
# 2  
Old 07-28-2010
Code:
sed -n "N;N;s/\n/|/g;p" file



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

Code:
paste -d"|" - - - < file

Or

Code:
awk ' { ORS=( NR % 3 == 0 ? "\n" : "|" ) } 1 ' file

This User Gave Thanks to anbu23 For This Post:
# 3  
Old 07-28-2010
MySQL

Code:
# 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
.....
.....

Code:
# 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|.....|.....


Code:
# ./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|.....|.....

Code:
 
# 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

# 4  
Old 07-28-2010
If the lines between abc... is not always two lines, you can try this code:

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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Concat data

All, I have 2 files A and B with some data. Now i want to concat data from both the files in to 3rd file.Please help me with a single command line. A--123456789 B--jlsjdfkajsjas output file C should be 123456789,jlsjdfkajsjas (2 Replies)
Discussion started by: kiranparsha
2 Replies

2. Shell Programming and Scripting

Concat name

Hi, I need help to combine the first 7 character of firstname if it is longer than 7and combine with the first character of lastname. ex: username lastname => usernaml user lastname => userl Thanks in advance. (10 Replies)
Discussion started by: xitrum
10 Replies

3. Shell Programming and Scripting

Concat String with variable after a 'grep' and awk

Here is the structure of my file: MyFile.txt g-4.n.g.fr 10.147.243.63 g-4.n.g.fr-w1 Here is my sript: test.sh #! /bin/sh ip=10.147.243.63 worker=$(grep -e $ip $1 | awk '{ print $3; }') echo "" echo $worker echo "" echo $worker echo "" echo "$worker.v.1" echo... (7 Replies)
Discussion started by: chercheur111
7 Replies

4. Shell Programming and Scripting

awk concat lines between 2 sequent digits

I would like to print string between two sequent digits and concatenate it into one single line. input.txt 99 cord, rope, strand, twine, twist, 100 strand, twine, twist, cord, rope 101 strand, twine, twist, twine, twist, cord, rope 105 cord, rope ,twi ... (8 Replies)
Discussion started by: sdf
8 Replies

5. Shell Programming and Scripting

Concat

Hi All, My Input file contains: Input.txt Name|Marks ABC|10 GHI|10 JKL|20 MNO|20 PQR|30 Output.txt MARKS|NAME 10|ABC,GHI 20|JKL,MNO 30|PQR Thanks in advance (4 Replies)
Discussion started by: kmsekhar
4 Replies

6. Shell Programming and Scripting

Conditional concat lines awk

Hello, I have a text file like this: NONE FILE_Rename frompath: /log_audit/AIX/log/current/AIXAFTPP.log NONE FILE_Unlink filename /audit/tempfile.14041142 NONE FILE_Rename ... (8 Replies)
Discussion started by: carloskl
8 Replies

7. Shell Programming and Scripting

concat 6 files in 1 file ( maybe use AWK?)

hi, I have the following problem: - 6 different files that have one key in common. - this six files must be aggregated in one output file sorted by the key. - the main file has to be writen twice, one in the beggining of the new output file and another in the end, for each key. - add one... (3 Replies)
Discussion started by: naoseionome
3 Replies

8. Shell Programming and Scripting

concat fields

hi I have a file, I need to concatenate depening on the no of columns i need to concatenate. for example i need to concatenate field1,filed34,field2( no of columns is not always 3, it can be any number of fields) concat.ksh field1 field34 field2 how to achieve this, is there any argv ,argc... (10 Replies)
Discussion started by: markjason
10 Replies

9. Shell Programming and Scripting

Concat

HI all, How to concat two strings in Shell scrpits suppose x=a y=b i want to display it as ab How to do it ? Thanks.. (1 Reply)
Discussion started by: dhananjaysk
1 Replies

10. UNIX for Dummies Questions & Answers

Concat date

How do I concat a date to a filename eg; filename: jjjrtup to become jjjrtup29052002 mv jjjrtup jjjrtup ? date what should ? be (2 Replies)
Discussion started by: drukkie
2 Replies
Login or Register to Ask a Question