String manipulation row by row


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting String manipulation row by row
# 1  
Old 03-11-2011
String manipulation row by row

Dear masters,

I stuck again in a very tricky situation,so need your valuable inputs. I have a file [idxout.txt as attached] having rows as below:

Code:
_Db
  _Database                 1023       1      1       1   17.0B     0.2     1.0
_Field
  _Field-Name                  3       2      2      11   56.2K    64.1     2.0
  _Field-Position              4       2      2       8   35.5K    55.7     2.1
  _File/Field                  2       2      2      11   47.1K    53.7     2.1

And I want to manipulate it to something as shown below:

Code:
_Db._Database                 1023       1      1       1   17.0B     0.2     1.0
_Field._Field-Name                  3       2      2      11   56.2K    64.1     2.0
_Field._Field-Position              4       2      2       8   35.5K    55.7     2.1
_Field._File/Field                  2       2      2      11   47.1K    53.7     2.1

FYI, changes are happening only on the column-1 and the rest data will be remain unchanged. Attaching the input file[idxout.txt] and required output file[reqidxout.txt].

Thanks for your support.

Last edited by joeyg; 03-11-2011 at 12:39 PM.. Reason: Please use CodeTags around scripts and data
# 2  
Old 03-11-2011
Quote:
Originally Posted by patric2326
And I want to manipulate it to something as shown below:

Code:
_Db._Database                 1023       1      1       1   17.0B     0.2     1.0
_Field._Field-Name                  3       2      2      11   56.2K    64.1     2.0
_Field._Field-Position              4       2      2       8   35.5K    55.7     2.1
_Field._File/Field                  2       2      2      11   47.1K    53.7     2.1

Code:
# awk '{if(NF==1){x=$0;next};sub($1,x"."$1);sub(/[ ]+/,"")}1' file
_Db._Database                 1023       1      1       1   17.0B     0.2     1.0
_Field._Field-Name                  3       2      2      11   56.2K    64.1     2.0
_Field._Field-Position              4       2      2       8   35.5K    55.7     2.1
_Field._File/Field                  2       2      2      11   47.1K    53.7     2.1

# 3  
Old 03-11-2011
It checks to see if field 2 exists and if it doesn't, assumes field 1 is the title and stores it in H. Otherwise it adds H"." to the front of the first field and prints the whole mess.
Code:
awk '{ if(!$2) {H=$1;} else { $1=H "." $1; print $0; } }' < data
_Db._Database 1023 1 1 1 17.0B 0.2 1.0
_Field._Field-Name 3 2 2 11 56.2K 64.1 2.0
_Field._Field-Position 4 2 2 8 35.5K 55.7 2.1
_Field._File/Field 2 2 2 11 47.1K 53.7 2.1

# 4  
Old 03-11-2011
String manipulation row by row

Perfect !!!

This is exactly I was looking for.

Thanks for your quick response.

RegardsSmilie
# 5  
Old 03-14-2011
String manipulation row by row

Quote:
Hi Corona688,

Their is slight change in the "idxout.txt" file and when I executed your command I'm getting weired output. And the output I require should be something as attached "reqidxout.txt". So can you please suggest any modification in your previous command.
FYI,changes are their only on the first column.

Thanks for your support.

Regards,
# 6  
Old 03-14-2011
Quote:
Hi Danmero,
I tried your option also but getting the syntax error as below:

>awk '{if(NF==1){x=$0;next};sub($1,x"."$1);sub(/[ ]+/,"")}1' idxout
awk: syntax error near line 1
awk: illegal statement near line 1
awk: syntax error near line 1
awk: illegal statement near line 1
awk: syntax error near line 1
awk: bailing out near line 1
Regards,
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Manipulation row order in file

Hello, I am trying to replace the position of each row by the next row. OS: Ubuntu 18.04, bionic I'd appreciate your help. input_file: -O fileA wget http://x.y.z./a -O fileB wget http://a.b.c./d -O fileC wget http://q.f.s/t .. .. .. -O fileZZ wget http://r.t.y/u I expect: (6 Replies)
Discussion started by: baris35
6 Replies

2. UNIX for Beginners Questions & Answers

Keep only the closet match of timestamped row (include headers) from file1 to precede file2 row/s

This is a question that is related to one I had last August when I was trying to sort/merge two files by millsecond time column (in this case column 6). The script (below) that helped me last august by RudiC solved the puzzle of sorting/merging two files by time, except it gets lost when the... (0 Replies)
Discussion started by: aachave1
0 Replies

3. Shell Programming and Scripting

Splitting single row into multiple rows based on for every 10 digits of last field of the row

Hi ALL, We have requirement in a file, i have multiple rows. Example below: Input file rows 01,1,102319,0,0,70,26,U,1,331,000000113200000011920000001212 01,1,102319,0,1,80,20,U,1,241,00000059420000006021 I need my output file should be as mentioned below. Last field should split for... (4 Replies)
Discussion started by: kotra
4 Replies

4. UNIX for Beginners Questions & Answers

Keep only the closet match of timestamped row (include headers) from file1 to precede file2 row/s

My original files are like this below and I distinguish them from the AP_ID (file1 has 572 and file2 has 544). Also, the header on file1 has “G_” pre-pended. NOTE: these are only snippets of very large files and much of the data is not present here. Original File 1: ... (36 Replies)
Discussion started by: aachave1
36 Replies

5. Shell Programming and Scripting

Add Row from First Row (Split Row)

HI Guys, I have Below Input :- RepigA_hteis522 ReptCfiEtrBsCll_aofe MSL04_MSL2_A25_1A 0 9 MSL04_MSL2_A25_1B 0 9 MSL04_MSL2_A25_1C 0 9 RepigA ReptCfiEtrBsCll hteis522 aofe MSL04_MSL2_A25_1A 0 9 MSL04_MSL2_A25_1B 0 9 MSL04_MSL2_A25_1C 0 9 Split Data in two first row... (2 Replies)
Discussion started by: pareshkp
2 Replies

6. Emergency UNIX and Linux Support

[Solved] Mysql - Take data from row and copy it to another row

Sorry if I repost my question in this section, but I'm really in a hurry since I have to finish my work... :( Dear community, I have a table with two rows like: Row1 Row2 ======= ======= 7,3 text 1 1,3 text 2 1,2,3 blabla What i need to do is add/copy... (2 Replies)
Discussion started by: Lord Spectre
2 Replies

7. Shell Programming and Scripting

Subtracting each row from the first row in a single column file using awk

Hi Friends, I have a single column data like below. 1 2 3 4 5 I need the output like below. 0 1 2 3 4 where each row (including first row) subtracting from first row and the result should print below like the way shown in output file. Thanks Sid (11 Replies)
Discussion started by: ks_reddy
11 Replies

8. UNIX for Dummies Questions & Answers

Shell Script: Traverse Database Table Row by Row

Hello Everyone, My issue is that I want to traverse a database table row by row and do some action on the value retrieved in each row. I have gone through a lot of shell script questions/posts. I could find row by row traversal of a file but not a database table. Please help. Thanks &... (5 Replies)
Discussion started by: ahsan.asghar
5 Replies

9. Shell Programming and Scripting

remove row if string is same as previous row

I have data like: Blue Apple 6 Red Apple 7 Yellow Apple 8 Green Banana 2 Purple Banana 8 Orange Pear 11 What I want to do is if $2 in a row is the same as $2 in the previous row remove that row. An identical $2 may exist more than one time. So the out file would look like: Blue... (4 Replies)
Discussion started by: dcfargo
4 Replies

10. Shell Programming and Scripting

Changing the column for a row in a text file and adding another row

Hi, I want to write a shell script which increments a particular column in a row from a text file and then adds another row below the current row with the incremented value . For Eg . if the input file has a row : abc xyz lmn 89 lm nk o p I would like the script to create something like... (9 Replies)
Discussion started by: aYankeeFan
9 Replies
Login or Register to Ask a Question