Help in creating a text file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help in creating a text file
# 1  
Old 07-05-2011
Help in creating a text file

Hi,

I need help in creating a file in specific format.
I have following lines in a file
Code:
0772
0ECC
0FC8
1080

1081
1082
1083
1084

1085
1086
1087
1088

1089
1159
115A
115B

115C
115D
115E
115F

1160
1161
1162
1163

1164
1165
1166
1167

116A
116B
116C
116D

As you see column is divided in to 4 elements. starting of line1,5,10,15,20 element should have the text " form meta from dev, "and end with config=stripe;
next 3 lines should be in the following format.
"add dev" at the stating of the line and they should use the letter in the first line in the example 0722 is used for next 3 lines.

I want the final o/p in the below format.
Code:
form meta from dev 0772,config=striped;
add dev 0ECC to meta 0772;             
add dev 0FC8 to meta 0772;             
add dev 1080 to meta 0772;             
                                       
form meta from dev 1081,config=striped;
add dev 1082 to meta 1081;             
add dev 1083 to meta 1081;             
add dev 1084 to meta 1081;             
                                       
form meta from dev 1085,config=striped;
add dev 1086 to meta 1085;             
add dev 1087 to meta 1085;             
add dev 1088 to meta 1085;             
                                       
form meta from dev 1089,config=striped;
add dev 1159 to meta 1089;             
add dev 115A to meta 1089;             
add dev 115B to meta 1089;             
                                       
form meta from dev 115C,config=striped;
add dev 115D to meta 115C;             
add dev 115E to meta 115C;             
add dev 115F to meta 115C;             
                                       
form meta from dev 1160,config=striped;
add dev 1161 to meta 1160;             
add dev 1162 to meta 1160;             
add dev 1163 to meta 1160;             
                                       
form meta from dev 1164,config=striped;
add dev 1165 to meta 1164;             
add dev 1166 to meta 1164;             
add dev 1167 to meta 1164;             
                                       
form meta from dev 116A,config=striped;
add dev 116B to meta 116A;             
add dev 116C to meta 116A;             
add dev 116D to meta 116A;

Please help me. I am new to shell programming. I can write in perl. I want to try with shell. I am getting any ideas on top of my head.

Thanks in advance.

Last edited by Franklin52; 07-05-2011 at 04:59 AM.. Reason: Please use code tags for code and data samples, thank you
# 2  
Old 07-05-2011
Code:
awk '
NR%4==1 {
  m=$0
  print "form meta from dev " m ",config=striped;"
  next
}
{ 
  print "add dev " $0 " to meta " m ";"
}' file

# 3  
Old 07-05-2011
YOu could write using the kornshell
Code:
#!/bin/ksh

while read A
do
    read B
    read C
    read D
    read EMPTY
    print "form meta from dev $A,config=striped;"
    print "add dev $B to meta $A;"
    print "add dev $C to meta $A;"
    print "add dev $D to meta $A;"
    print
done

To execute this script, run:
Code:
./name_of_the_script <inputfile >outputfile

# 4  
Old 07-05-2011
The same in bash:
Code:
#!/bin/bash
while read a; read b;read c;read d; do
  read nl;
  echo "form meta from dev $a,config=striped;"
  for i in $b $c $d; do
    echo "add dev $i to meta $a;"
  done
  echo
done <$1

Run as ./script.sh file
# 5  
Old 07-05-2011
Code:
 
$ nawk ' { if (NR%5==1) {base=$0;printf("form meta from dev %s,config=striped;\n",$0);for(i=0;i<3;i++){getline;printf("add dev %s to meta %s;\n",$0,base);}}}' inputfile

form meta from dev 0772,config=striped;
add dev 0ECC to meta 0772;
add dev 0FC8 to meta 0772;
add dev 1080 to meta 0772;
form meta from dev 1081,config=striped;
add dev 1082 to meta 1081;
add dev 1083 to meta 1081;
add dev 1084 to meta 1081;
form meta from dev 1085,config=striped;
add dev 1086 to meta 1085;
add dev 1087 to meta 1085;
add dev 1088 to meta 1085;
form meta from dev 1089,config=striped;
add dev 1159 to meta 1089;
add dev 115A to meta 1089;
add dev 115B to meta 1089;
form meta from dev 115C,config=striped;
add dev 115D to meta 115C;
add dev 115E to meta 115C;
add dev 115F to meta 115C;
form meta from dev 1160,config=striped;
add dev 1161 to meta 1160;
add dev 1162 to meta 1160;
add dev 1163 to meta 1160;
form meta from dev 1164,config=striped;
add dev 1165 to meta 1164;
add dev 1166 to meta 1164;
add dev 1167 to meta 1164;
form meta from dev 116A,config=striped;
add dev 116B to meta 116A;
add dev 116C to meta 116A;
add dev 116D to meta 116A;

# 6  
Old 07-05-2011
Through Sed..
Code:
# Pre-requisite: last line of the file to be blank
sed -n 'H;/^$/{x;s/\n\(.*\)\n\(.*\)\n\(.*\)\n\(.*\)\n/form meta from dev \1,config=striped;\nadd dev \2 to meta \1;\nadd dev \3 to meta \1;\nadd dev \4 t0 meta \1;\n/p}' inputfile

# 7  
Old 07-05-2011
Thanks a lot!! This is amazing.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Creating new users using a text file as imput (using only shell script and/or awk)

I need somebody who could help with an exercise. You have a text file called users.txt with this info inside: users.txt: user1:1234:/home/homedir1 ; user2:1234:/home/homedir2 ; user3:1234:/home/homedir3 ; user4:1234:/home/homedir4 ; The script should create an user using the... (2 Replies)
Discussion started by: marcosruiz
2 Replies

2. Shell Programming and Scripting

crontab not creating text file

while running the following code in ssh crontab, Its running successfully but its not creating text file IFC1.txt, file1.txt, file2.txt while running manually its working. please help me #!/bin/ksh hostname > file1.txt 2>/dev/null hostname >> file1.txt 2>/dev/null sudo df -h | grep... (2 Replies)
Discussion started by: elango963
2 Replies

3. Shell Programming and Scripting

Creating a new file with each line of a text file

Hello, I would like to make a script in c-shell, using sed comand, but i don know how to do this task: I have a file name data in /home/user/IMPUT with several text lines: 1144088 2012/05/21 23:20:08 36.8505 -11.2618 0 4.3(mb) 1143593 2012/05/18 13:12:12 34.0526 ... (3 Replies)
Discussion started by: tenteyu
3 Replies

4. Shell Programming and Scripting

Help for reformatting text file and creating new format

Hi all, I have an input file like 1,date,company,, 1,date,comapny,, 2,000,,,567,ACT,00,,,,KKG,M1,D45,,67J,+4500000000 2,000,,,567,ACT,00,,,,KKG,M6,D49,,56J,+6000 2,000,,,567,ACT,00,,7,,KKG,M3,D58,,68h,-70000 2,000,,,567,ACT,00,,,,KKG,M9,D95,,34m,0.00 3,total what i require is 1.I... (2 Replies)
Discussion started by: selvankj
2 Replies

5. Shell Programming and Scripting

KSH - help needed for creating a script to generate xml file from text file

Dear Members, I have a table in Oracle DB and one of its column name is INFO which has data in text format which we need to fetch in a script and create an xml file of a new table from the input. The contents of a single cell of INFO column is like: Area:app - aam Clean Up Criteria:... (0 Replies)
Discussion started by: Yoodit
0 Replies

6. UNIX for Dummies Questions & Answers

creating text file with content from script

hi, can somebody tell me how I can create a text file with content from Bash script. The file should be prefilled with information such as current date and time then leaving the user ability to input more data right below those prefilled content. thank you :) (0 Replies)
Discussion started by: s3270226
0 Replies

7. Shell Programming and Scripting

Help with creating a text file in perl with file creation date.

Hi, I am quite new to Perl scripting and i need to create a .TXT file using perl, with fields (A,B,C,D,E), and this text file should be named with current file creation date "XYZ_CCYYMMDD.TXT" (i.e.XYZ_2011042514:33 PM). Can anyone who has done this, please share their expertise on this... (5 Replies)
Discussion started by: msrahman
5 Replies

8. Homework & Coursework Questions

creating search script for a text file

I am aware of the stipulations regarding homework, however I am completely stuck and do not know how to even begin the following (in bash): Create a script that searches for a text file with most occurrences of a given keyword. Any help is greatly appreciated. Thank you (1 Reply)
Discussion started by: hybridoutlaw
1 Replies

9. Shell Programming and Scripting

Creating a text file in Local Drive

Hi All, I am new in Shell Script. I have a ksh script running in the Unix Server and basically in that script I need to create a text file but the text file has to be generated in the local PC (the user computer such as in C:\ drive). I have no idea on how to do it and I need it pretty urgently.... (2 Replies)
Discussion started by: yramli
2 Replies

10. UNIX for Dummies Questions & Answers

Creating flat text file (ASCII)

Hi everybody. I need help and I hope someone is willing to help me out here. My wholesale company is currently moving to new software. The old software is running on a UNIX platform. We need to migrate data from the UNIX system, but our former software provider refuses to assist the data... (5 Replies)
Discussion started by: Wdonero
5 Replies
Login or Register to Ask a Question