Visit Our UNIX and Linux User Community


Looking into file !


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Looking into file !
# 8  
Old 10-03-2007
Hi.

This script creates another script. The new script uses sed, so it is quite fast, and the entire process is data-driven by the lookup file:
Code:
#!/usr/bin/env sh

# @(#) s1       Demonstrate creation of sed script with awk.

set -o nounset
echo

## Use local command version for the commands in this demonstration.

echo "(Versions used in this script displayed with local utility "version")"
version bash awk sed

echo

echo " Input file of lookup tokens:"
cat -tv data1

./a1 data1 > script
chmod +x script

echo
echo " This is the created sed script:"
cat -n script

echo
echo " Results of running script:"
./script data2

exit 0

You notice that there is an "a1" script. It's in awk, because awk facilitates handling text fields. However, the quoting is a bit complicated:
Code:
#!/usr/bin/awk -f

# @(#) a1       Demonstrate creation of sed script from lookup file.

echo

BEGIN { print "sed \\" }
        { print "-e 's/" $1 " /" $1$2 " /' \\" }
END     { print " data2" }

When s1 runs, it then produces:
Code:
% ./s1

(Versions used in this script displayed with local utility version)
GNU bash, version 2.05b.0(1)-release (i386-pc-linux-gnu)
GNU Awk 3.1.4
GNU sed version 4.1.2

 Input file of lookup tokens:
6589 7879
8787 0909
4343 4576

 This is the created sed script:
     1  sed \
     2  -e 's/6589 /65897879 /' \
     3  -e 's/8787 /87870909 /' \
     4  -e 's/4343 /43434576 /' \
     5   data2

 Results of running script:
6767879898009965897879 65656576687878
7887576576757687870909 88787878756446
3232476568769843434576 42341242542345

Best wishes ... cheers, drl
# 9  
Old 10-03-2007
Quote:
Originally Posted by drl
Hi.

This script creates another script. The new script uses sed, so it is quite fast, and the entire process is data-driven by the lookup file:
Code:
#!/usr/bin/env sh

# @(#) s1       Demonstrate creation of sed script with awk.

set -o nounset
echo

## Use local command version for the commands in this demonstration.

echo "(Versions used in this script displayed with local utility "version")"
version bash awk sed

echo

echo " Input file of lookup tokens:"
cat -tv data1

./a1 data1 > script
chmod +x script

echo
echo " This is the created sed script:"
cat -n script

echo
echo " Results of running script:"
./script data2

exit 0

You notice that there is an "a1" script. It's in awk, because awk facilitates handling text fields. However, the quoting is a bit complicated:
Code:
#!/usr/bin/awk -f

# @(#) a1       Demonstrate creation of sed script from lookup file.

echo

BEGIN { print "sed \\" }
        { print "-e 's/" $1 " /" $1$2 " /' \\" }
END     { print " data2" }

When s1 runs, it then produces:
Code:
% ./s1

(Versions used in this script displayed with local utility version)
GNU bash, version 2.05b.0(1)-release (i386-pc-linux-gnu)
GNU Awk 3.1.4
GNU sed version 4.1.2

 Input file of lookup tokens:
6589 7879
8787 0909
4343 4576

 This is the created sed script:
     1  sed \
     2  -e 's/6589 /65897879 /' \
     3  -e 's/8787 /87870909 /' \
     4  -e 's/4343 /43434576 /' \
     5   data2

 Results of running script:
6767879898009965897879 65656576687878
7887576576757687870909 88787878756446
3232476568769843434576 42341242542345

Best wishes ... cheers, drl
I haven't seen anything like this before. Very cool. I will need to play with this. Smilie
# 10  
Old 10-04-2007
awk!!!

hi,

input:
Code:
a:
6589 7879
8787 0909
4343 4576

b:
67678798980099 6589 65656576687878
78875765767576 8787 88787878756446
32324765687698 4343 42341242542345

output:
Code:
67678798980099 6589 7879 65656576687878
78875765767576 8787 0909 88787878756446
32324765687698 4343 4576 42341242542345

code:
Code:
awk '{
if (NF==2)
a[$1]=$0
else
print $1 " "a[$2] " "$3
}' a b

# 11  
Old 10-04-2007
summer_cherry,
this is not the expected input for file 'b' - reread the OP.

Plus, there's a better/preferable way to distiguish between 2 input file: rather than relying on different number of fields (as is the case for this particular example) - take advantage of NR and FNR values when processing 2 input files. There're plenty of examples in the previous awk-related solutions/threads.
# 12  
Old 10-04-2007
Situation has changed ! :(

This time file_to_be_processed has values like this:

1US146576287192498004994 0 0 0
1US144566547890498004994 0 0 0
1US123443212330498004994 0 0 0

lookup_file:
4657628719 1231231234
4456654789 6788769890
2344321233 2345678900

Now I need to replace bold values from file_to_be_processed by corresponding value in lookup_file.

Plz Help.
# 13  
Old 10-04-2007
Code:
BEGIN {
   _start=5
   _length=10
}
FNR==NR {f1[$1]=$2; next}
{
  s=substr($1, _start, _length)
  if ( s in f1)
     $1 = substr($1, 1, _start-1) f1[s] substr($1,_start+_length)
}
1

# 14  
Old 10-04-2007
small issue

It's working perfectly fine !
Tons and Tons of thanks ....
But only 1 small issue came Smilie

I am loosing the original blank spaces in the final output :
So if in main file values are like this :
1US6786868976897[space][space][space]0[space][space]0
1US6786868976897[space][space][space]0[space][space]0
1US6786868976897[space][space][space]0[space][space]0
My out put after processing is coming like this:
1US6786868976897[space]0[space]0
1US6786868976897[space]0[space]0
1US6786868976897[space]0[space]0
So multiple space is getting truncated:
How to solve this?

Also I need to save the output in a separate file ...Kindly suggest !

Previous Thread | Next Thread
Test Your Knowledge in Computers #138
Difficulty: Easy
The Unix philosophy is a computer operating system idea that means the operating system provides a set of basic tools that perform a limited, well-defined function, a unified filesystem as the main method of communication, and a shell scripting and command language to combine modular tools to perform more complex workflows.
True or False?

3 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script (sh file) logic to compare contents of one file with another file and output to file

Shell script logic Hi I have 2 input files like with file 1 content as (file1) "BRGTEST-242" a.txt "BRGTEST-240" a.txt "BRGTEST-219" e.txt File 2 contents as fle(2) "BRGTEST-244" a.txt "BRGTEST-244" b.txt "BRGTEST-231" c.txt "BRGTEST-231" d.txt "BRGTEST-221" e.txt I want to get... (22 Replies)
Discussion started by: pottic
22 Replies

2. Shell Programming and Scripting

Compare 2 text file with 1 column in each file and write mismatch data to 3rd file

Hi, I need to compare 2 text files with around 60000 rows and 1 column. I need to compare these and write the mismatch data to 3rd file. File1 - file2 = file3 wc -l file1.txt 58112 wc -l file2.txt 55260 head -5 file1.txt 101214200123 101214700300 101250030067 101214100500... (10 Replies)
Discussion started by: Divya Nochiyil
10 Replies

3. Shell Programming and Scripting

Match list of strings in File A and compare with File B, C and write to a output file in CSV format

Hi Friends, I'm a great fan of this forum... it has helped me tone my skills in shell scripting. I have a challenge here, which I'm sure you guys would help me in achieving... File A has a list of job ids and I need to compare this with the File B (*.log) and File C (extend *.log) and copy... (6 Replies)
Discussion started by: asnandhakumar
6 Replies

Featured Tech Videos