awk to reformat text


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk to reformat text
# 1  
Old 12-19-2013
awk to reformat text

I have this input and want output like below, how can I achieve that through awk:

Input:

Code:
CAT1 FRY-01
CAT1 FRY-04
CAT1 DRY-03
CAT1 FRY-02
CAT1 DRY-04
CAT2 FRY-03
CAT2 FRY-02
CAT2 DRY-01
FAT3 DRY-12
FAT3 FRY-06


Output:

Code:
category CAT1
item FRY-01
item FRY-04
item DRY-03
item FRY-02
item DRY-04
ok

category CAT2
item FRY-03
item FRY-02
item DRY-01
ok

category FAT3
item DRY-12
item FRY-06
ok

# 2  
Old 12-19-2013
Hello,

Not a single liner but may help you.

Code:
value=`awk '!a[$1]++' check_remove_dupli_1st | awk '{print $1}'`
set -A array_value $value
for i in ${array_value[@]}
do
echo "catagory" $i
while read line
do
str=`cat check_remove_dupli_1st | grep "$i" | awk '{print $2}'`
set -A array_str $str
for j in ${array_str[@]}
do
echo item $j
done
echo "ok"
break;
done < "check_remove_dupli_1st"
done


Output will be as follows.

Code:
$ ksh check_remove_dupli_1st.ksh
catagory CAT1
item FRY-01
item FRY-04
item DRY-03
item FRY-02
item DRY-04
ok
catagory CAT2
item FRY-03
item FRY-02
item DRY-01
ok
catagory FAT3
item DRY-12
item FRY-06
ok



NOTE: where check_remove_dupli_1st is the file name which have input.


Thanks,
R. Singh

Last edited by RavinderSingh13; 12-19-2013 at 07:56 AM.. Reason: adding code to put ok at output
# 3  
Old 12-19-2013
Or try:
Code:
awk 'cat!=$1{if(cat){print "ok" RS} cat=$1; print "category ", $1} {print "item", $2}END{print "ok"}' file

These 2 Users Gave Thanks to Franklin52 For This Post:
# 4  
Old 12-19-2013
If field1 is sorted then this would work

Code:
$  cat <<test | awk '$1!=p{print NR == 1 ? "category" OFS $1 : "ok" RS RS "category" OFS $1}{print "item",$2;p=$1}END{print "ok"}'
CAT1 FRY-01
CAT1 FRY-04
CAT1 DRY-03
CAT1 FRY-02
CAT1 DRY-04
CAT2 FRY-03
CAT2 FRY-02
CAT2 DRY-01
FAT3 DRY-12
FAT3 FRY-06
test

category CAT1
item FRY-01
item FRY-04
item DRY-03
item FRY-02
item DRY-04
ok

category CAT2
item FRY-03
item FRY-02
item DRY-01
ok

category FAT3
item DRY-12
item FRY-06
ok

This User Gave Thanks to Akshay Hegde For This Post:
# 5  
Old 12-19-2013
Code:
awk '$1 != last && NR != 1 { print "ok\n"; } $1 != last { print "category " $1; last = $1;}{print "item " $2}END{print "ok"}' filename

# 6  
Old 12-19-2013
Quote:
Originally Posted by Franklin52
Or try:
Code:
awk 'cat!=$1{if(cat){print "ok" RS} cat=$1; print "category ", $1} {print "item", $2}END{print "ok"}' file

can you explain what that means it can be helpful

---------- Post updated at 05:38 PM ---------- Previous update was at 05:37 PM ----------

Quote:
Originally Posted by Akshay Hegde
If field1 is sorted then this would work

Code:
$  cat <<test | awk '$1!=p{print NR == 1 ? "category" OFS $1 : "ok" RS RS "category" OFS $1}{print "item",$2;p=$1}END{print "ok"}'
CAT1 FRY-01
CAT1 FRY-04
CAT1 DRY-03
CAT1 FRY-02
CAT1 DRY-04
CAT2 FRY-03
CAT2 FRY-02
CAT2 DRY-01
FAT3 DRY-12
FAT3 FRY-06
test

category CAT1
item FRY-01
item FRY-04
item DRY-03
item FRY-02
item DRY-04
ok

category CAT2
item FRY-03
item FRY-02
item DRY-01
ok

category FAT3
item DRY-12
item FRY-06
ok


can explain the working of the code it would be helpful
# 7  
Old 12-19-2013
Quote:
Originally Posted by zozoo
can you explain what that means it can be helpful
Code:
awk 'cat!=$1{if(cat){print "ok" RS} cat=$1; print "category ", $1} {print "item", $2}END{print "ok"}' file

Explanation:
Code:
if variable cat != $1 then
  if variable cat is not empty then
    print "ok" and RS
  end if
  variable cat = $1
  print "category " and $1
end if
print "item" and $2

END {print "ok"}

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to reformat text file

Howdy. AWK beginner here. I need to reformat a text file in the following format: TTGS08-2014001 6018.00 143563.00 ... (2 Replies)
Discussion started by: c47v3770
2 Replies

2. Shell Programming and Scripting

Reformat awk output

I need to rearrange the output but i am unable to arrange it to match the format. In the output i need NAME=\"To in the column . Bash: #!/bin/bash cd /cygdrive/c/output/a cat *.txt > output.txt i=/cygdrive/c/output/a/output.csv #echo "NE_Name, Source, Destination, OSPF_AREA_ID"... (4 Replies)
Discussion started by: adgjmpt
4 Replies

3. Shell Programming and Scripting

Using awk to reformat file output

Hi there. I need to reformat a large file. Here is a sample of the file. NETIK0102_UCS_Boot_a,NETIK0102_UCS_Boot_b 5200 2438 70G 5200 2439 70G NETIK0102_UCS_HBA0_a,NETIK0102_UCS_HBA1_b,NETIK0102_UCS_HBA2_a,NETIK0102_UCS_HBA3_b 2673 19D7 55G 2673 19C0 30G 2673 19F5 120G... (5 Replies)
Discussion started by: kieranfoley
5 Replies

4. Shell Programming and Scripting

awk reformat file

Hello: When I tried a perl-oneliner to re-format fasta file. infile.fasta >YAL069W-1.334 Putative promoter CCACACCACACCCACACACC ACACCACACCCACACACACA ACAGCCCTAATCTAACCC >YAL068C-7235.2170 Putative ABC sequence TACGAGAATAATTT ACGTAAATGAAGTT TATATATAAA >gi|31044174|gb|AY143560.1|... (15 Replies)
Discussion started by: yifangt
15 Replies

5. Shell Programming and Scripting

need awk or sed help to reformat output

We have the following output: server1_J00_data_20120711122243 server1_J00_igs_20120711122243 server1_J00_j2ee_20120711122243 server1_J00_sec_20120711122243 server1_J00_data_20120711131819 server1_J00_igs_20120711131819 server1_J00_j2ee_20120711131819 server2_J00_data_20120711122245... (10 Replies)
Discussion started by: ux4me
10 Replies

6. Shell Programming and Scripting

Reformat MLS Data - Use AWK?

I am helping my wife set up a real estate site and I am starting to integrate MLS listings. We are using a HostGator level 5 VPS running CentOS and have full root and SSH access to the VPS. Thus far I have automated the daily FTP download of listings from our MLS server using a little sh script.... (4 Replies)
Discussion started by: Chicago_Realtor
4 Replies

7. Shell Programming and Scripting

awk to reformat a text file

I am definitely not an expert with awk, and I want to reformat a text file like the following. This is probably a very easy one for an expert out there. I would like to keep the lines in the same order, but move the heading to only be listed once above the lines. This is what the text file... (7 Replies)
Discussion started by: linux4life
7 Replies

8. Shell Programming and Scripting

Reformat text table

Hello, I have a challenge here to reformat a text table. The original table follows: Item01: m1, m2, m3: A; m4, m5, m6: B; m7, m8: C; m9 m10: D Item02: m1, m9, m10: A; m7, m5, m6: C; m2, m3, m4, m8: D Item03: m1, m6, m7: A; m2: B; m3, m4: C; m5 m8 m9 m10: D . . . Please note: 1)... (7 Replies)
Discussion started by: yifangt
7 Replies

9. Shell Programming and Scripting

help reformat data with awk

I am trying to write an awk program to reformat a data table and convert the date to julian time. I have all the individual steps working, but I am having some issues joing them into one program. Can anyone help me out? Here is my code so far: # This is an awk program to convert the dates from... (4 Replies)
Discussion started by: climbak
4 Replies
Login or Register to Ask a Question