Help with extracting a part of a line between two patterns


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with extracting a part of a line between two patterns
# 1  
Old 04-16-2012
Help with extracting a part of a line between two patterns

Hello All,

I have a text file with contents as below:
Code:
 
contents of error.txt:
message1="Reason for error code1"
message2="Reason for error code2"
message3="Reason for error code3.
                    To solve this, you may try doing restart"

I have a requirement where in I have to extract the contents between the " and save it in a variable for each error code and use it later in my shell script.
(Please note that the messages may be spanned across multi-lines between " as for message3.)

I managed to do it something like:

Code:
 
#!/bin/ksh
## some code here
 
value=`sed -n '/code3/{N;s/.*"\(.*\)".*/\1/p}'  error.txt`
echo "Reason for error code3 is:$value"



Code:
 
Output should look like:
user1@linux:/home/user1> ./error_message.ksh
Reason for error code3 is:Reason for error code3.
                    To solve this, you may try doing restart

This works in ksh in linux OS. But, I am facing problem in executing the same peice of code in AIX. SmilieSmilie
Quote:
Error message says:
sed: Function /code3/{N;s/.*"\(.*\)".*/\1/p} cannot be parsed
Please help..!!

Thanks in anticipation.
# 2  
Old 04-16-2012
just source it.
Code:
$ cat var.txt
message1="Reason for error code1"
message2="Reason for error code2"
message3="Reason for error code3.
                    To solve this, you may try doing restart"

#Source the file var.txt
$ . var.txt 

$ echo $message1
Reason for error code1

$ echo $message2 
Reason for error code2

$ echo $message3
Reason for error code3. To solve this, you may try doing restart

This User Gave Thanks to itkamaraj For This Post:
# 3  
Old 04-16-2012
Thanks for the reply.

As per my requirement, the txt file cannot be executable. So I cannot source it. I have to just read it.Smilie
Is there any other way that I could get the values of the variables.

PS: The "new line character" in the value should be preserved. The value for message3 should come in two lines only.
# 4  
Old 04-16-2012
Sourcing a file does not require it to be executable.

To preserve the carriage return, enclose the variable in quotes:

Code:
echo "$message3"

Code:
    .  filename [arguments]
     source filename [arguments]
          Read and execute commands from filename in the  current
          shell  environment  and  return  the exit status of the
          last command executed from filename.  If filename  does
          not  contain  a  slash,  file names in PATH are used to
          find  the  directory  containing  filename.   The  file
          searched for in PATH need not be executable.  When bash
          is not in posix mode, the current directory is searched
          if  no file is found in PATH.  If the sourcepath option
          to the shopt builtin command is turned off, the PATH is
          not  searched.   If  any  arguments  are supplied, they
          become the positional parameters when filename is  exe-
          cuted.    Otherwise   the   positional  parameters  are
          unchanged.  The return status is the status of the last
          command  exited within the script (0 if no commands are
          executed), and false if filename is not found or cannot
          be read.

This User Gave Thanks to gary_w For This Post:
# 5  
Old 04-18-2012
Thanks.

This made it so simple Smilie
I was under the impression that the file should be an executable to source it.

I have learnt something new Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find matched patterns and print them with other patterns not the whole line

Hi, I am trying to extract some patterns from a line. The input file is space delimited and i could not use column to get value after "IN" or "OUT" patterns as there could be multiple white spaces before the next digits that i need to print in the output file . I need to print 3 patterns in a... (3 Replies)
Discussion started by: redse171
3 Replies

2. Shell Programming and Scripting

[Solved] Printing a part of the last line of the specific part of a file

Hi, I have 80 large files, from which I want to get a specific value to run a Bash script. Firstly, I want to get the part of a file which contains this: Name =A xxxxxx yyyyyy zzzzzz aaaaaa bbbbbb Value = 57 This is necessary because in a file there are written more lines which... (6 Replies)
Discussion started by: wenclu
6 Replies

3. Shell Programming and Scripting

Extracting a set of patterns from the text file

Hi experts, I need a help in extracting a set of patterns from the text file. Below is my scenario. Input file: I need to extract the data between My output should be as Thanks, Kalai (7 Replies)
Discussion started by: kalpeer
7 Replies

4. Shell Programming and Scripting

extracting part of a line excluding particular word from it

here is the line on which i want to process `empNo` int(13) NOT NULL AUTO_INCREMENT, it sometimes doesnt have comma at the end too `empNo` int(13) NOT NULL AUTO_INCREMENT i want to extract all except "AUTO_INCREMENT" not only this line i ,want the code to work on any line if it has... (5 Replies)
Discussion started by: vivek d r
5 Replies

5. Shell Programming and Scripting

Extracting text between two patterns 1 and 2 and pattern2 should be second occurrence of the file

Hi All, I have a small query. I have a file containing the following lines File 1: 29-Jul-2011 GMT Static data requires update <Extraction should start here> ----------- ----------- -------------------- ----------------------- ----------- <should stop here> Pattern1 will be time... (2 Replies)
Discussion started by: gangii87
2 Replies

6. UNIX for Dummies Questions & Answers

Locating and Extracting Specific Patterns from a file

Hi all, 1. I have a file that is getting continously refreshed (appended) I want to grep all the strings containing substring of the type abcdf123@aaa.xxx.yyy.zzz:portnumber: where, before @, any letters or numbers combination, after @, IP address then symbol : then port... (4 Replies)
Discussion started by: kokoras
4 Replies

7. Shell Programming and Scripting

Extracting part of line between two words

Hi, I have a file few hundred MB's with text like one below in single line. 20091117 abc xyg 20091117 def ghi 20091118 ppp ttt 20091118 zzz zzz xxx I need to extract part of line from 1st occurence of pattern 20091117 till first occurence of another pattern 20091118. I tried... (3 Replies)
Discussion started by: artistic94555
3 Replies

8. Shell Programming and Scripting

need help extracting this part

JADE TRADER 143W MYPEN 40 HC M X10 28 7 1 0 MYPEN 20 GP X X10 15 2 1 0 MYPEN 40 GP X X10 28 7 1 0 MYPEN 20... (6 Replies)
Discussion started by: finalight
6 Replies

9. Shell Programming and Scripting

Extracting part of a string

Hi all, I have to extract only the second part of a database column (VARCHAR) and the value is seperated by a "~" xyz~ chxyz36r~ abder~000082685 mnops~000083554 fulfil302~00026 Above are some examples of the values and for each record I have to extract the value after "~" , if there is a... (8 Replies)
Discussion started by: sam_78_nyc
8 Replies
Login or Register to Ask a Question