Relocation strings using awk/sed from a index file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Relocation strings using awk/sed from a index file
# 1  
Old 05-10-2016
Relocation strings using awk/sed from a index file

Hi All,

I'd always appreciate all helps from this website. I would like to relocate strings based on the index number from an index file.


Index numbers are shown on the first column in the index file (index.txt) and I would like to relocate "path" based on index numbers. Paths are placed in the same row if the index number is the same. For example, there are two zeros so path_sparc_ifu_dec_in_3826 is placed on the first row and path_sparc_ifu_dec_in_4349 is placed on the first row and next to path_sparc_ifu_dec_in_3826.

index.txt:

Code:
     0        path_sparc_ifu_dec_in_3826  str    DR     -         -
     0        path_sparc_ifu_dec_in_4349  stf    DR     -         -
     1        path_sparc_ifu_dec_in_2374  stf    DR     -         -
     1        path_sparc_ifu_dec_in_4011  stf    DR     -         -
     2        path_sparc_ifu_dec_in_3078  stf    DR     -         -

However, strings are written in another file (source.txt) and each "path" has four lines of strings.

source.txt:

Code:
    path_sparc_ifu_dec_in_3826
    dtu_inst_d[14]
    dec_fcl_rdsr_sel_pc_d
    0.8664
    path_sparc_ifu_dec_in_4349
    dtu_inst_d[18]
    dec_swl_rdsr_sel_thr_d
    0.795429
    path_sparc_ifu_dec_in_2374
    dtu_inst_d[13]
    dec_dcl_cctype_d[2]
    0.938914
    path_sparc_ifu_dec_in_4011
    dtu_inst_d[13]
    ifu_exu_useimm_d
    0.843643
    path_sparc_ifu_dec_in_3078
    dtu_inst_d[12]
    ifu_exu_shiftop_d[2]
    0.915818

The desired output is:

Code:
    path_sparc_ifu_dec_in_3826	    path_sparc_ifu_dec_in_4349
    dtu_inst_d[14]	    dtu_inst_d[18]
    dec_fcl_rdsr_sel_pc_d	    dec_swl_rdsr_sel_thr_d
    0.8664	0.795429
    path_sparc_ifu_dec_in_2374	    path_sparc_ifu_dec_in_4011
    dtu_inst_d[13]	    dtu_inst_d[13]
    dec_dcl_cctype_d[2]	    ifu_exu_useimm_d
    0.938914	0.843643
    path_sparc_ifu_dec_in_3078	
    dtu_inst_d[12]	
    ifu_exu_shiftop_d[2]	
    0.915818

My idea is that (1)combining two files first and (2) relocate path info using the index number, but I don't know how to do this work. Probably, sed/awk is an appropriate language.

Any help is appreciated.

Best,

Jaeyoung
# 2  
Old 05-10-2016
How about
Code:
awk '
NR==FNR         {T[$2] = $1
                 MX = $1
                 next
                }
$1 in T         {IX = T[$1]
                }
                {P[IX, (FNR+3)%4] = P[IX, (FNR+3)%4] $0
                }
END             {for (i=0; i<=MX; i++) for (j=0; j<4; j++) print P[i, j]
                }
' file[12]
    path_sparc_ifu_dec_in_3826    path_sparc_ifu_dec_in_4349
    dtu_inst_d[14]    dtu_inst_d[18]
    dec_fcl_rdsr_sel_pc_d    dec_swl_rdsr_sel_thr_d
    0.8664    0.795429
    path_sparc_ifu_dec_in_2374    path_sparc_ifu_dec_in_4011
    dtu_inst_d[13]    dtu_inst_d[13]
    dec_dcl_cctype_d[2]    ifu_exu_useimm_d
    0.938914    0.843643
    path_sparc_ifu_dec_in_3078
    dtu_inst_d[12]
    ifu_exu_shiftop_d[2]
    0.915818

# 3  
Old 05-10-2016
Thank you. I think I need replace file[12] with index.txt and source.txt. Is that correct?


Code:
awk '
NR==FNR         {T[$2] = $1
                 MX = $1
                 next
                }
$1 in T         {IX = T[$1]
                }
                {P[IX, (FNR+3)%4] = P[IX, (FNR+3)%4] $0
                }
END             {for (i=0; i<=MX; i++) for (j=0; j<4; j++) print P[i, j]
                }
' index.txt source.txt

# 4  
Old 05-11-2016
What happens if you do?
# 5  
Old 05-11-2016
nothing to show. Is there a bash shell/cshell issue? I will try it with cshell.
# 6  
Old 05-11-2016
No, no - it should run on bash! And yes - file1 is index.txt, and file2 is source.txt. "nothing to show" means - no output?
# 7  
Old 05-11-2016
Hi RudiC,

I have changed a server to run this code and get the desired result. One quick question is how to insert a tab between strings. I tried print P[i, j]"\t" and print "\t"P[i, j], but did not work.

Thank you.

My result:
Code:
path_sparc_ifu_dec_in_3826path_sparc_ifu_dec_in_4349
dtu_inst_d[14]dtu_inst_d[18]
dec_fcl_rdsr_sel_pc_ddec_swl_rdsr_sel_thr_d
0.86640.795429
path_sparc_ifu_dec_in_2374path_sparc_ifu_dec_in_4011
dtu_inst_d[13]dtu_inst_d[13]
dec_dcl_cctype_d[2]ifu_exu_useimm_d
0.9389140.843643
path_sparc_ifu_dec_in_3078
dtu_inst_d[12]
ifu_exu_shiftop_d[2]
0.915818


Last edited by RudiC; 05-11-2016 at 01:22 PM.. Reason: Added icode tags
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Adding sequential index to duplicate strings

I have a text file in the following format >Homo sapiens KQKCLYNLPFKRNLEGCRERCSLVIQIPRCCKGYFGRDCQACPGGPDAPCNNRGVCLDQY SATGECKCNTGFNGTACEMCWPGRFGPDCLPCGCSDHGQCDDGITGSGQCLCETGWTGPS CDTQAVLPAVCTPPCSAHATCKENNTCECNLDYEGDGITCTVVDFCKQDNGGCAKVARCS... (2 Replies)
Discussion started by: jerrild
2 Replies

2. UNIX for Beginners Questions & Answers

Use strings from nth field from one file to match strings in entire line in another file, awk

I cannot seem to get what should be a simple awk one-liner to work correctly and cannot figure out why. I would like to use patterns from a specific field in one file as regex to search for matching strings in the entire line ($0) of another file. I would like to output the lines of File2 which... (1 Reply)
Discussion started by: jvoot
1 Replies

3. Shell Programming and Scripting

Relocation strings

Hi all, I would like to relocate strings based on the index number. Index numbers are shown on the first column, the strings are shown on the second column. 1 path_sparc_ifu_dec_104 1 path_sparc_ifu_dec_105 2 path_sparc_ifu_dec_63 2 ... (3 Replies)
Discussion started by: jypark22
3 Replies

4. Programming

How to replace the complex strings from a file using sed or awk?

Dear All, I am having a requirement to find the difference between 2 files and generate a discrepancy report out of it as an html page. I prefer using diff -y file1 file2 since it gives user friendly layout to know any discrepancy in the record and unique records among the 2 file. Here's how it... (12 Replies)
Discussion started by: Badhrish
12 Replies

5. Shell Programming and Scripting

awk or sed script to remove strings

Below am trying to separate FA-7A:1, In output file it should display 7A 1 Command am using Gives same output as below format: 22B7 10000000c9720873 0 22B7 10000000c95d5d8b 0 22BB 10000000c97843a2 0 22BB 10000000c975adbd 0 Not showing FA ports as required format... (5 Replies)
Discussion started by: aix_admin_007
5 Replies

6. Shell Programming and Scripting

awk extract certain digits from file with index substr

I would like to extract a digit from $0 starting 2,30 to 3,99 or 2.30 to 3.99 Can somebody fix this? awk --re-interval '{if($0 ~ /{1}{2}/) {print FILENAME, substr($0,index($0,/{1}{2}/) , 4)}}'input abcdefg sdlfkj 3,29 g. lasdfj alsdfjasl 2.86 gr. slkjds sldkd lskdjfsl sdfkj kdjlksj 3,34 g... (4 Replies)
Discussion started by: sdf
4 Replies

7. Shell Programming and Scripting

Replace Strings with sed or awk

Hello i need some help with the usage of sed. Situation : 2 textfiles, file.in , file.out In the first textfile which is called file.in are the words for the substitution. Every word is in a new-line like : Firstsub Secondsub Thridsub ... In the second textflie wich is called file.out is... (5 Replies)
Discussion started by: Kingbruce
5 Replies

8. UNIX for Dummies Questions & Answers

Using awk/sed to extract text between Strings

Dear Unix Gurus, I've got a data file with a few hundred lines (see truncated sample)... BEGIN_SCAN1 TASK_NAME=LA48 PDD Profiles PROGRAM=ArrayScan 1.00 21.220E+00 2.00 21.280E+00 END_DATA END_SCAN1 BEGIN_SCAN2 TASK_NAME=LA48 PDD Profiles 194.00 2.1870E+00 ... (5 Replies)
Discussion started by: tintin72
5 Replies

9. Filesystems, Disks and Memory

why the inode index of file system starts from 1 unlike array index(0)

why do inode indices starts from 1 unlike array indexes which starts from 0 its a question from "the design of unix operating system" of maurice j bach id be glad if i get to know the answer quickly :) (0 Replies)
Discussion started by: sairamdevotee
0 Replies

10. Shell Programming and Scripting

(awk) compare files in dir with an index file

Using awk I have an index file which has been seperated into 5 fields. The first field contains file names. What I need to do is check to see if a file exists in my current directory that is not in the first field of my index file. If its not i print out a message. Please help! (4 Replies)
Discussion started by: xthexonex
4 Replies
Login or Register to Ask a Question