Sponsored Content
Top Forums Shell Programming and Scripting Search: find current line, then search back Post 302879662 by alister on Friday 13th of December 2013 03:18:49 PM
Old 12-13-2013
Quote:
Originally Posted by in2nix4life
Not a one-liner, but it does the job
I disagree. One of the few things we know about the data is that a matching pair is ordered. Your code does not guarantee that a last fetch precedes its new = 0 counterpart.


Quote:
Originally Posted by in2nix4life
Code:
# get current working directory and
# change to it
cwd=$(pwd)
cd $cwd

Why change to the current directory? It's already the current directory. Am I missing something?

There is actually a bug in that cd. If the current directory is foo bar, and if there exists a directory named foo, cd $cwd will change to an unintended directory, because the variable expansion isn't double-quoted.


Quote:
Originally Posted by in2nix4life
Code:
for p in ${PATTERN[@]}
do
    ...
    awk "/$srchPattern/{print}" $f
done

If there are P patterns, P+1 passes will be necessary. Hopefully it's a small dataset with few patterns. Your approach could be reimplemented to always finish in 2 passes.


Quote:
Originally Posted by in2nix4life
Code:
    p=$(echo $p | sed 's#\([]/&[]\)#\\\1#g')

Escaping characters for a regular expression engine is a bug-prone procedure that yields brittle solutions.

A case in point: sed 's#\([]/&[]\)#\\\1#g' will add a backslash before each ampersand. Since ampersands are not an AWK ERE metacharacter, \& is an undefined sequence. An AWK implementation is allowed to either silently ignore the backslash or it can choose to abort or it can do ... whatever it wants.

I prefer to avoid this type of manual escaping as much as possible.

Regards,
Alister
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Question about Restricting Search path of FIND to current directory

Hi, By default FIND command searches for matching files in all the subdirectories within the specified path. Is there a way to restrict FIND command's search path to only the specified directory and NOT TO scan its subdirectories. Any help would be more than appreciated. Thanks and Regards (2 Replies)
Discussion started by: super_duper_guy
2 Replies

2. Shell Programming and Scripting

Perl: Search for string on line then search and replace text

Hi All, I have a file that I need to be able to find a pattern match on a line, search that line for a text pattern, and replace that text. An example of 4 lines in my file is: 1. MatchText_randomNumberOfText moreData ReplaceMe moreData 2. MatchText_randomNumberOfText moreData moreData... (4 Replies)
Discussion started by: Crypto
4 Replies

3. Shell Programming and Scripting

search for the matched pattern by tracing back from the line

Hi, I want to grep the line which has 'data11'.then from that line, i need to trace back and find out the immediate line which has the same timestamp of that grepped line. for eg: log file: ----------- Process - data Process - datavalue - 2345 Process - data Process - data Process... (9 Replies)
Discussion started by: Sharmila_P
9 Replies

4. Shell Programming and Scripting

search directory-find files-append at end of line

Hi, I have a command "get_data" with some parameters in few *.text files of a directory. I want to first find those files that contain this command and then append the following parameter to the end of the command. example of an entry in the file :- get_data -x -m50 /etc/web/getid this... (1 Reply)
Discussion started by: PrasannaKS
1 Replies

5. Shell Programming and Scripting

How to use sed to search for string and Print previous two lines and current line

Hello, Can anybody help me to correct my sed syntax to find the string and print previous two lines and current line and next one line. i am using string as "testing" netstat -v | sed -n -e '/test/{x;2!p;g;$!N;p;D;}' -e h i am able to get the previous line current line next line but... (1 Reply)
Discussion started by: nmadhuhb
1 Replies

6. Shell Programming and Scripting

search for difference and find the above line

hello All, i have to two files package.today and package.yesterday , the extension of the files says what day the file belongs to . contents of the file change little bit everyday . i am taking only the package and sub package from the files awk '/^/{print $0}' Packages.today 82 ... (0 Replies)
Discussion started by: posner
0 Replies

7. Shell Programming and Scripting

perl search and replace - search in first line and replance in 2nd line

Dear All, i want to search particular string and want to replance next line value. following is the test file. search string is tmp,??? ,10:1 "???" may contain any 3 character it should remain the same and next line replace with ,10:50 tmp,123 --- if match tmp,??? then... (3 Replies)
Discussion started by: arvindng
3 Replies

8. UNIX for Dummies Questions & Answers

Restricting a Find search to the current directory only

Hi All, I am trying to delete file (with a mtime older than 2 days) from the current directory ONLY using: find . -daystart -maxdepth 1 -mtime 2 -exec rm {} \; but this doesn't seem to work it is still find files in subdirectories which I don't want to delete. Please can anyone offer... (2 Replies)
Discussion started by: daveu7
2 Replies

9. Shell Programming and Scripting

Search several string and convert into a single line for each search string using awk command AIX?.

I need to search the file using strings "Request Type" , " Request Method" , "Response Type" and by using result set find the xml tags and convert into a single line?. below are the scenarios. Cat test Nov 10, 2012 5:17:53 AM INFO: Request Type Line 1.... (5 Replies)
Discussion started by: laknar
5 Replies

10. Shell Programming and Scripting

How to restrict Find only search the current directory?

hello, all I have googled internet, read the man page of Find, searched this forum, but still could not figure out how. My current directory is: little@wenwen:~$ pwd /home/little little@wenwen:~$ I want to use find command to list the files in my current directory, how should i write... (3 Replies)
Discussion started by: littlewenwen
3 Replies
All times are GMT -4. The time now is 10:54 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy