Extract string from multiple file based on line count number


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extract string from multiple file based on line count number
# 8  
Old 04-04-2011
Quote:
Originally Posted by cgkmal
Code:
awk '{gsub(/-/," -");a[FNR]=a[FNR]?a[FNR] FS $3:$3}END{for (i=1;i in a;i++) print a[i]}' FILE* # With gsub()

That won't work because of the "-" used by the exponential notation. See my post for a gsub() sequence that should work.

Regards,
Alister
# 9  
Old 04-04-2011
Quote:
Originally Posted by alister
That won't work because of the "-" used by the exponential notation. See my post for a gsub() sequence that should work.

Regards,
Alister
Right!,

Thanks for your observation
Alister, I had not considered the "E-[0-9][0-9]" part.
The option would be:
Code:
awk '{$0=gensub(/([0-9])-/,"\\1 -","g");a[FNR]=a[FNR]?a[FNR] FS $3:$3}END{for (i=1;i in a;i++) print a[i]}' FILE* 

or a little bit shorter:
awk '{$0=gensub(/([0-9])-/,"\\1 -","g");a[FNR]=a[FNR]?a[FNR] FS $3:$3}END{for(i in a) print a[i]}' FILE*

Regards
# 10  
Old 04-04-2011
Quote:
Originally Posted by cgkmal
[COLOR=Navy]
awk '{$0=gensub(/([0-9])-/,"\\1 -","g");a[FNR]=a[FNR]?a[FNR] FS $3:$3}END{for(i in a) print a[i]}' FILE*

[/CODE]Regards
This will lost original sequence, you can't short it by this way.

That's why I use below code.
Code:
for (i=1;i in a;i++)

This User Gave Thanks to rdcwayx For This Post:
# 11  
Old 04-04-2011
@ rdcwayx : Thanks a million. Your solution working perfectly.

@ alister, cgkmal : Thanks for showing other way. Now I should learn more. Thanks you.

@ pravin27 : Your script makes an inconsistency result between data after line 2 to below.. Thanks anyway for using perl as an option.

THANK YOU VERY MUCH FOR ALL OF YOU.
# 12  
Old 04-04-2011
Quote:
Originally Posted by rdcwayx
This will lost original sequence, you can't short it by this way.


Thanks, something new I learned
Smilie

Regards
# 13  
Old 04-07-2011
try below perl

my @arr;
my @files = glob("*.txt");
foreach my $f (@files){
open FH,"<$f";
while(<FH>){
my @tmp = split(/(?Smilie?<=[0-9])(?=-)|\s+)/g,$_);
$arr[$.]= $arr[$.]." ".$tmp[2];
}
close FH;
}
for(my $i=1;$i<=$#arr;$i++){
print $arr[$i],"\n";
}
# 14  
Old 04-07-2011
@cgkmal
@rdcwayx
@alister

I just wanted to pointed out a potential unexpected behaviour :

If one of the considered a[FNR] fields of the scanned files has the value 0

the code :

Code:
a[FNR]=a[FNR]?a[FNR] FS $3:$3

will behave a wrong way :

a[FNR]? will be evaluated to "false" so that the a[FNR] will be reset to $3 and may lose the previous concatenated values which doesn't fit with what we are looking for ...
These 2 Users Gave Thanks to ctsgnb 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

Count number of occurrence of a string in file

if there's a file containing: money king money queen money cat money also money king all those strings are on one line in the file. how can i find out how many times "money king" shows up in the line? egrep -c "money king" wont work. (7 Replies)
Discussion started by: SkySmart
7 Replies

2. Shell Programming and Scripting

HELP: Shell Script to read a Log file line by line and extract Info based on KEYWORDS matching

I have a LOG file which looks like this Import started at: Mon Jul 23 02:13:01 EDT 2012 Initialization completed in 2.146 seconds. -------------------------------------------------------------------------------- -- Import summary for Import item: PolicyInformation... (8 Replies)
Discussion started by: biztank
8 Replies

3. Shell Programming and Scripting

Merge two non-consecutive lines based on line number or string

This is a variation of an earlier post found here: unixcom/shell-programming-scripting/159821-merge-two-non-consecutive-lines.html User Bartus11 was kind enough to solve that example. Previously, I needed help combining two lines that are non-consecutive in a file. Now I need to do the... (7 Replies)
Discussion started by: munkee
7 Replies

4. Shell Programming and Scripting

extract a line from a file by line number

Hi guys, does anyone know how to extract(grep) a line from the file, if I know the line number? Thanks a lot. (9 Replies)
Discussion started by: aoussenko
9 Replies

5. Shell Programming and Scripting

How to count number of occurances of string in a file?

Gurus, Need little guidance. I have A.txt and B.txt file. B.txt file contains Unique strings. Sample content of B.txt file for which i cut the fourth column uniquely and output directed to B.txt file And A.txt file contains the above string as a fourth column which is last column. So A.txt... (7 Replies)
Discussion started by: Shirisha
7 Replies

6. Shell Programming and Scripting

How to extract specific data and count number containing sets from a file?

Hello everybody! I am quit new here and hope you can help me. Using an awk script I am trying to extract data from several files. The structure of the input files is as follows: TimeStep parameter1 parameter2 parameter3 parameter4 e.g. 1 X Y Z L 1 D H Z I 1 H Y E W 2 D H G F 2 R... (2 Replies)
Discussion started by: Daniel8472
2 Replies

7. UNIX for Dummies Questions & Answers

Extract a specific number from an XML file based on the start and end tags

Hello People, I have the following contents in an XML file ........... ........... .......... ........... <Details = "Sample Details"> <Name>Bob</Name> <Age>34</Age> <Address>CA</Address> <ContactNumber>1234</ContactNumber> </Details> ........... ............. .............. (4 Replies)
Discussion started by: sushant172
4 Replies

8. Shell Programming and Scripting

extract a line from a file using the line number

Hello, I am having trouble extracting a specific line from a file when the line number is known. My first attempt involved grep -n 'hi' (the word 'hi will always be there) to get the line number before the line that I actually want (line 4). Extra Notes: -I am working in a bash script. -The... (7 Replies)
Discussion started by: grandtheftander
7 Replies

9. Shell Programming and Scripting

Extract a line from a file using the line number

I have a shell script and want to assign a value to a variable. The value is the line exctrated from a file using the line number. The line number it is not fix, and could change any time. I have tried sed, awk, head .. See my script # Get randome line number from the file #selectedline = `awk... (1 Reply)
Discussion started by: zambo
1 Replies

10. UNIX for Dummies Questions & Answers

count the number of files which have a search string, but counting the file only once

I need to count the number of files which have a search string, but counting the file only once if search string is found. eg: File1: Please note that there are 2 occurances of "aaa" aaa bbb ccc aaa File2: Please note that there are 3 occurances of "aaa" aaa bbb ccc... (1 Reply)
Discussion started by: sudheshnaiyer
1 Replies
Login or Register to Ask a Question