awk or sed to format text file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk or sed to format text file
# 1  
Old 12-29-2010
awk or sed to format text file

hi all,

i have a text file which looks like the below
Code:
01 
02 
abc Top 40
music
Kidz Only!
MC 851
MC 852
MC 853
7NOW
Arch_Diac
xyz2
abc
h211
Commacc1
Commacc2
Commacc3
5 max
5 max HD
@MAX
@MAX HD
Action MA
08 
09 
10 
BBC World News 
BET ON

here if anything starting with number and then have character next to it must be converted into character and then number
if its just a number then it must left as it is

eg

"7NOW" must be converted to NOW7 and "5 max" must be converted to "max 5" (max5 doesnt matter)


the output must look like this
Code:
01
02
abc Top 40
music
Kidz Only!
MC 851
MC 852
MC 853
NOW7
Arch_Diac
xyz2
abc
h211
Commacc1
Commacc2
Commacc3
max 5
max HD 5
@MAX
@MAX HD
Action MA
08 
09 
10 
BBC World News 
BET ON

Thanks
# 2  
Old 12-29-2010
Hi

Try this perl snippet:
Code:
[root@gpr ~]# cat f1.pl
#!/usr/bin/perl -n

chop;
if (/^(\d+) (\D+)$/){
  print $2, " ", $1, "\n";
}elsif (/^(\d+)([a-zA-Z]+)$/){
        print $2,$1,"\n";
}else{
       print $_, "\n";;
}
[root@gpr ~]#

Run: (where a is the input file)
Code:
[root@gpr ~]# ./f1.pl a
01
02
abc Top 40
music
Kidz Only!
MC 851
MC 852
MC 853
NOW7
Arch_Diac
xyz2
abc
h211
Commacc1
Commacc2
Commacc3
max 5
max HD 5
@MAX
@MAX HD
Action MA
08
09
10
BBC World News
BET ON
[root@gpr ~]#


Guru.
# 3  
Old 12-29-2010
Thanks Guru,

UR script almost works but i have some issue when i extended the file little

Code:
01
02
7now
abc Top 40
music
Kidz Only!
MC 851
MC 852
MC 853
7NOW
Arch_Diac
xyz2
abc
h211
Commacc1
Commacc2
Commacc3
5 max
5 max HD
@MAX
@MAX HD
Action MA
08
09
10
7now
BBC World News
MC9
Mexicana
Singers and Standard
11
7NOW
Commacc1
Commacc2
22
7NOW
13
11
7NOW
5 MAX HD
5 MAX HD
BET ON

the output i got is

Code:
./f1.pl test.txt 
01 
02 
now7
abc Top 40
music
Kidz Only!
MC 851
MC 852
MC 853
NOW7
Arch_Diac
xyz2
abc
h211
Commacc1
Commacc2
Commacc3
max 5
max HD 5
@MAX
@MAX HD
Action MA
08 
09 
10 
now7
BBC World News 
MC9
Mexicana
Singers and Standard
11 
7NOW                  # this is not getting changed for some reason 
Commacc1 
Commacc2 
22
NOW7
13
11
NOW7
MAX HD 5
MAX HD 5
BET ON

Thanks
# 4  
Old 12-29-2010
Hi
When I run, I am getting the right output.

Guru
This User Gave Thanks to guruprasadpr For This Post:
# 5  
Old 12-29-2010
Try this,
Code:
perl -nle 's/^(\d+\s+|\d)(\D+)/\2 \1/g;print $_;' inputfile

This User Gave Thanks to pravin27 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Format the text using sed or awk

I was able to figure out how to format a text. Raw Data: $ cat test Thu Aug 23 15:43:28 UTC 2018, hostname01, 232.02, 3, 0.00 Thu Aug 23 15:43:35 UTC 2018, hostname02, 231.09, 4, 0.31 Thu Aug 23 15:43:37 UTC 2018, hostname03, 241.67, 4, 0.43 (5 Replies)
Discussion started by: kenshinhimura
5 Replies

2. Shell Programming and Scripting

Datestamp format 2nd change in csv file (awk or sed)

I have a csv file formatted like this: 2014-08-21 18:06:26,A,B,12345,123,C,1232,26/08/14 18:07and I'm trying to change it to MM/DD/YYYY HH:MM for both occurances. I have got this: awk -F, 'NR <=1 {print;next}{"date +%d/%m/%Y\" \"%H:%m -d\""$1 "\""| getline dte;$1=dte}1' OFS="," test.csvThis... (6 Replies)
Discussion started by: say170
6 Replies

3. Shell Programming and Scripting

Help on Log File format using sed or awk

Hello Gurus, First, i would like to know is there any way to solve my problem. i have a log file like this: INFO - ABCDRequest :: processing started for the record <0> TransactionNo <Txn#1> recordID <recID#1> INFO - ABCDRequest :: processing started for the record <0> TransactionNo... (9 Replies)
Discussion started by: VasuKukkapalli
9 Replies

4. Shell Programming and Scripting

Help with awk statement to format text file.

Hello, I am fairly new to shellscripting and have written a script to check on messages file and report failed logins: Here is the original file: Jul 17 03:38:07 sfldmilx086 sshd: error: PAM: Authentication failure for houghn97 from 10.135.77.201 Jul 17 03:38:07 sfldmilx086 sshd: error:... (2 Replies)
Discussion started by: neilh1704
2 Replies

5. UNIX for Dummies Questions & Answers

Inserting text into a file with awk or sed

Hello, I've been trying to get a script working that fetches weather-data and converts it into an .ics file. The script works so far put I'm stuck at the point where I need to add specific static data. A thorough search through the forum did not point me into the right direction. #!/bin/bash... (3 Replies)
Discussion started by: Schubi
3 Replies

6. Shell Programming and Scripting

using sed/awk to replace a block of text in a file?

My apologies if this has been answered in a previous post. I've been doing a lot of searching, but I haven't been able to find what I was looking for. Specifically, I am wondering if I can utilize sed and/or awk to locate two strings in a file, and replace everything between those two strings... (12 Replies)
Discussion started by: kiddsupreme
12 Replies

7. Shell Programming and Scripting

Need awk/sed to format a file

My content of source file is as below scr1 a1 scr2 a2 b2 scr3 a3 b3 c3 I need a awk/sed command (to be used in C shell)to format it to something like below scr1 $a1 >file1 scr2 $a2 $b2 >file2 scr3 $a3 $b3 $c3 >file3 (12 Replies)
Discussion started by: animesharma
12 Replies

8. Shell Programming and Scripting

Joining lines in a text file using AWK or SED

Hi All I'm struggling a bit here :( I need a way of joining lines contained in a text file. I've seen numerous SED and AWK examples and none of them seem to be working for me. The text file has 4 lines: DELL1427 DOC 30189342 79 Now bear with me on this one as I'm actually... (4 Replies)
Discussion started by: huskie69
4 Replies

9. Programming

awk script to convert a text file into csv format

hi...... thanks for allowing me to start a discussion i am collecting usb usage details of all users and convert it into csv files so that i can export it into some database.. the input text file is as follows:- USB History Dump by nabiy (c)2008 (1) --- Kingston DataTraveler 130 USB... (2 Replies)
Discussion started by: certteam
2 Replies

10. Shell Programming and Scripting

awk or sed to add field in a text file

Hi there, I have a csv file with some columns comma sepated like this : 4502-17,PETER,ITA2,LEGUE,92,ME - HALF,23/05/10 15:00 4502-18,CARL,ITA2,LEGUE,96,ME - HALF,20/01/09 14:00 4502-19,OTTO,ITA2,LEGUE,97,ME - MARY,23/05/10 15:00 As you can see the column n. 7 is a timestamp column, I need... (23 Replies)
Discussion started by: capnino
23 Replies
Login or Register to Ask a Question