Hello.
I would like to write a bash function which would return "true" if the search succeed else return anything else.
something like
$1 : The file, where to search in, is a text file.
$2 : must start at the beginning of a line and can contains / or ' like :
$3 : must begin at the beginning of a line, or begin with a tab, or begin with a space like :
$4 : is any single word or init 3
$5 : is a single character but could be }
Last edited by Don Cragun; 09-08-2013 at 03:01 PM..
Reason: Change Bold text to ICODE tags; add CODE tags.
I find your description cryptic. Please provide a few sample input files, sample arguments to your function for those input files, and the output you want to have produced by your function for each of those samples.
You have sed in the title of this thread, but it sounds like this might be easier using awk. Will you accept a solution using something other than sed?
I know little about sed, I know nothing about awk.
I have try this :
In the following text sample :
Now calling the function with these parameters
This find $1 in file $2, and then find $3 before $5.
This give a few lines beginning with $3 and last line ending with $5
Now searching with $4 = manages in the first line will return true
and searching with $4 = background in the first line will return false
Thank you for taking time to help me.
Moderator's Comments:
Please use CODE tags (not QUOTE tags) to mark sample input and output.
Last edited by Don Cragun; 09-08-2013 at 09:04 PM..
Reason: Change QUOTE tags to CODE tags.
I know little about sed, I know nothing about awk.
That didn't answer the question. Will you accept a solution for this problem that uses awk?
Quote:
Originally Posted by jcdole
I have try this :
In the following text sample :
Now calling the function with these parameters
This find $1 in file $2, and then find $3 before $5.
This give a few lines beginning with $3 and last line ending with $5
There are a few problems here:
You're using the wrong quotes to do what you want. (Positional parameters are not expanded inside single-quoted strings.)
Your quotes aren't matched. (You have 5 single quotes in these 3 sed commands.)
In the 1st message in this thread you said the 1st argument to your function was the file name; here it is the 2nd argument. Which should it be?
In the 1st message in this thread you said the 5th argument was a single character; "ways" is not a single character. What is the real requirement?
In the 1st message in this thread you said the 3rd argument had to appear at the start of a line, after a <space> character, or after a <tab> character. Your script seems to only allow for the 1st of these three options. Is this still a requirement?
Quote:
Originally Posted by jcdole
Now searching with $4 = manages in the first line will return true
and searching with $4 = background in the first line will return false
Thank you for taking time to help me.
Do you really need to return the strings "true" and "false"; or are exit codes zero and non-zero, respectively, sufficient.
Hello.
I would like to write a bash function which would return "true" if the search succeed else return anything else.
something like
$1 : The file, where to search in, is a text file.
$2 : must start at the beginning of a line and can contains / or ' like :
$3 : must begin at the beginning of a line, or begin with a tab, or begin with a space like : $4 : is any single word or init 3
$5 : is a single character but could be }
This is the set of requirements you stated in your first message in this thread. Since then you have switched the 1st two arguments to your function (ignoring my question about whether or not that was intentional). The requirements in red above are completely ignored in your recent shell functions.
Do you still care about these any of these original requirements?
My last post show what I have tried before I post my question.
And the pseudo code show exactly the problem:
1°) There is one parameter ($1) for the file name : "/root/Documents/file_text.txt" in the example
2°) There is one parameter ($2) for the first token : "In the Linux startup process" in the example
So we can ignore the begin of the text file
3°) There is one parameter ($3) for the second token : "Like init"
So we can search safely from this second token because we are protected by the first token and because we know that the second token appear only once after the first token. So we can ignore all the text until this second token after having found the first token.
4°) There is one parameter ($5) for the last token : "ways" in the example
So we stop searching beyond this token.
As I have no idea how to pass the character " \ " or " } " or " ' " or " . " as token, I have make my test with a single word.
5°) There is one parameter ($4) for the token to search for : "manage" in the example.
This token could start at the very beginning of a line, after a blank or after a tab ( some thing like [ ^ | \t | ]).
As I don't know how to represent this ORed expression, I give my example with a simple word.
As you can see, my pseudo code is unchanged.
What I would like is :
1°) make simpler
2°) how to pass a \, a }, a ' as $5
3°) how to pass [^ | \t | space ]token_to_pass as $4
Hi All,
I am new to scripting.
I have a requirement where I need to search for token present in array in a file. If the token is found I need to take the first column of that line from the file and append it to token and direct it to a new file.
e.g. token file
token.txt contains
abc... (2 Replies)
Hi, i have a strange prob. log file contains ip, protocol, user name, agent . these can be in any order. If log contains the above order able to fetch all details but if details are in diff order not able to fetch all details.
using below command.
grep -A50 "Entry " "/logs/file.log" \
|grep... (6 Replies)
Hello.
The token is any printable characters between 2 " .
The token is unknown, but we know that it is between 2 "
Tok 1 : "1234x567"
Tok 2 : "A3b6+None"
Tok 3 : "A3b6!1234=@"
The ligne is :
Line 1 :
"9876xABCDE"Do you have any code fragments or data samples in your post
Line 2 : ... (3 Replies)
Hi again
I have figured out how to be able to sort through lines in a file with multiple words in any order and display them using this command:
cat file | grep -i $OPTION1 | grep -i $OPTION2 | grep -i $OPTION3 OPTION1 is 2008, OPTION2 is Mar, OPTION 3 is Tue
Result:
Tue Mar 25... (4 Replies)
Hi,
Please take a look at the below eg. I would like to search for abc() pattern first and then search for (xyz) in the next line. If I can find the pattern, then I should delete the 3 lines.
I can only find the pattern and delete but I am unable to find two patterns and delete. Any... (8 Replies)
Hi -
I have a file with lots of lines in that I need to order based on the number of commas!
e.g the file looks something like :-
cn=john,cn=users,cn=uk,dc=dot,dc=com
cn=john,cn=users,dc=com
cn=users,cn=groups,dc=com
cn=john,cn=admins,cn=users,cn=uk,dc=dot,dc=com... (4 Replies)
Im trying to develop a shell script that will change the content order of the file.
For example I have a file that says
a
b
c
d
I want to change this to be
d
c
b
a
Im trying to use sed to this by reading the file and then inserting each line at the top
#!/usr/bin/ksh
... (3 Replies)
hi,
i'm making now a bash script, that runs some compiler... i want to take only errors form its output eg:
output:
bla bla bla
...
erros is 1324546
the bla bla bla
bla bla bla...
...
and i want to get only
erros is 1324546
the bla bla bla (11 Replies)
I have a text
"abc def ghi"
and I want to get it as
"def abc ghi"
I am using this
echo "abc def ghi" | sed 's/\(*\)\(*\)/\2\1/'
But I am not able to get the output, could anyone help me.
Thanks (9 Replies)
so i have been trying to learn how to manipulate text on my own and have gotten stumped...
let's say i have a text file that says (highly simplified):
people ordinary
How would swap the order of the words..
I know i need to use sed and some kind of back reference but cannot make it... (2 Replies)