Include strings between a pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Include strings between a pattern
# 1  
Old 03-12-2015
Include strings between a pattern

Hi,

I have two files:

File1:

Code:
sample statements1

<UID = " "  PWD = " ">

sample statements2

File2:

Code:
UID ="admin"
PWD ="password"


I have to read UID and PWD from file2 and place them in file1 in between those quotes.

file1 after update:

Code:
sample statements1

<UID = "admin"  PWD = "password">

sample statements2

Thanks,
Mani

Last edited by manid; 03-12-2015 at 04:16 AM..
# 2  
Old 03-12-2015
Is it just one line? Or the pattern is unique?

Code:
UID="admin"
PWD="password"

Code:
sed 's/^<UID.*PWD.*/UID = '\"$UID\"' PWD = '\"$PWD\"'>/' file

This User Gave Thanks to clx For This Post:
# 3  
Old 03-12-2015
Is this a homework assignment?

Will UID and PWD always be on the same line in File1?
Do you really want the spaces between the quotes in the updated File1?
Can UID or PWD appear more than once in File1?

What have you tried?
# 4  
Old 03-12-2015
Yes the pattern is unique. I have to place the values read from file2 into file between those quotes. After update file1 should look like this

Code:
sample statements1

<UID = "admin"  PWD = "password">

sample statements2


Last edited by manid; 03-12-2015 at 04:17 AM..
# 5  
Old 03-12-2015
I repeat:
Is this a homework assignment?
What have you tried?
# 6  
Old 03-12-2015
Hi Don,

Its not what you are thinking. Am just trying it with shell. I just want to know whether it is possible in shell or not.
My approach is that i will store the UID and PWD as variables in file2. I will write a script file3.sh

file3.sh:
Code:
. file2
sed /replace the pattern from file1 with values in file 2. //use sed here.. I donno using sed command



<UID = "admin"  PWD = "password">

No need of any spaces. it should look like this.

Thanks,

Last edited by Don Cragun; 03-12-2015 at 04:31 AM.. Reason: Add CODE tags.
# 7  
Old 03-12-2015
Quote:
Originally Posted by manid
Hi Don,

Its not what you are thinking. Am just trying it with shell. I just want to know whether it is possible in shell or not.
My approach is that i will store the UID and PWD as variables in file2. I will write a script file3.sh

file3.sh:
Code:
. file2
sed /replace the pattern from file1 with values in file 2. //use sed here.. I donno using sed command



<UID = "admin"  PWD = "password">

No need of any spaces. it should look like this.

Thanks,
In the 1st post in this thread you said File2 (note the uppercase F) contains the code:
Code:
UID ="admin"
PWD ="password"

You haven't told us what OS or shell you're using, but there are at least two problems here:
  1. A file named File2 can't be loaded using the command . file2. (On UNIX and Linux systems, file names are case sensitive.)
  2. The commands in File2 are not assignment statements. They invoke the utility named UID with the operand -admin and the utility named PWD with the operand =password.
If you set the contents of file2 to be valid shell variable assignments as clx suggested:
Code:
UID="admin"
PWD="password"

(Note that there are no spaces around the = in either of these commands), then the following commands in file3 should do what you want:
Code:
#!/bin/ksh
. file2
sed 's/UID.*".*".*PWD.*".*"/UID = "'"$UID"'"  PWD = "'"$PWD"'"/' file1 > file1.$$ && mv file1.$$ file1

I generally prefer using the Korn shell, but this should work with any POSIX conforming shell or any shell that accepts basic Bourne shell syntax.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to include file pattern in find command?

Hi I've to remove the files which has the following file pattern in path /home/etc/logs fnm_HST_date1 fnm_hst_date1 fnm_HST_date2 I've used the following code to to remove the files having file names like "HST" . #!/usr/bin/ksh set -x file_path=/home/etc/logs file_nm=HST find... (2 Replies)
Discussion started by: smile689
2 Replies

2. Shell Programming and Scripting

how to include a text pattern in system function in PERL

Pleeeeease help.. I'm working in perl i have a system function to check whether a file is readable for others or not. i just want to print a text in that command my command is : system ("ls -la $filename | awk '\$1~ /^-......r../ {print \$9}'"); i know for displaying text in awk... (6 Replies)
Discussion started by: shubhamsachdeva
6 Replies

3. Shell Programming and Scripting

Sorting alphanumeric strings without a pattern

Good evening to all!! I'm facing this problem: I saved in a txt a list of files name (one txt for every folder): hello0.jpg hello1.jpg hello10.jpg hello11.jpg hello12.jpg hello13.jpg hello14.jpg hello15.jpg hello16.jpg hello17.jpg hello18.jpg hello19.jpg hello2.jpg hello20.jpg... (32 Replies)
Discussion started by: silver18
32 Replies

4. Shell Programming and Scripting

Recursive search on pattern between two strings

Objective: Recursively search all files under a directory for SQL statements that end with ";" Sample input: UPDATE table1 set col=val UPDATE table2 set cola=vala ,colb=valb; UPDATE table3 set col=val Expected output: UPDATE table2 set cola=vala ,colb=valb; (1 Reply)
Discussion started by: krishmaths
1 Replies

5. UNIX for Dummies Questions & Answers

Extraction of strings from a file, after pattern matching

I need to extract strings from a file. The file contains data like: Plan ABCD IN-+-172BB---118C2C---GGN_342-+-MM77_23--+-LAS24_3|GGK_774 | | \-LAS24_2|GGN_774 | +-AA_800_1-+-BAS_000|GGK_362 | | \-BAS_001|GGK_360 | \-DD_000T1---DAM_001|STEEL_0 Plan SHELL_1... (3 Replies)
Discussion started by: abkush
3 Replies

6. UNIX for Advanced & Expert Users

Remove rows which include the word pattern

Hi, I'm trying to remove rows that including specific word, but the below is replacing only the first occurrence, how to do this for the all occurrences within the file. grep -v "mdsMVLink\|mdsMVMembership\|mdsMVLinkType" sfile.txt > dfile.txt Could someone help me how to accomplish this? ... (2 Replies)
Discussion started by: john_prince
2 Replies

7. Shell Programming and Scripting

Extracting the strings matching a pattern from a word

Hi All , I need to extract the strings that are matching with the pattern : CUST.<AnyStringOfAnyLength>.<AnyStringOfAnyLength> from a file and then write all these string into another file. e.g. If a file SOURCE contains following lines : IF(CUST.ABCD.EFGH==1) THEN CUST.ABCD.EFGH =... (7 Replies)
Discussion started by: swapnil.nawale
7 Replies

8. Shell Programming and Scripting

Include Line Before Pattern Using Sed / Awk

Hi, I have a sql file that runs something like this vi Test.sql REVOKE EXECUTE ON DEMO_USER.SQC_SAMP FROM PUBLIC; REVOKE EXECUTE ON DEMO_USER.SQC_SAMP FROM DEMO_READ; REVOKE SELECT ON DEMO_USER.DEMO_NOMINEE_TEST FROM DEMO_READ; REVOKE EXECUTE ON DEMO_USER.SQC_SAMP FROM... (3 Replies)
Discussion started by: rajan_san
3 Replies

9. UNIX for Dummies Questions & Answers

Finding Unique strings which match pattern

I need to grep for a pattern in a file. Files are huge and have several repeated occurances of the strings which match pattern. I just need the strings which contain the pattern in the output. For eg. The contents of my file are as follows. The pattern I want to match by is ABCD ... (5 Replies)
Discussion started by: tektips
5 Replies

10. Shell Programming and Scripting

Extracting lines between 2 strings only if the pattern patches

Hi Friends, Please help me with the following problem: I have an xml file with the following lines: <init-param> <param-name>/default/directory</param-name> <param-value>default</param-value> </init-param> <init-param> ... (5 Replies)
Discussion started by: simran
5 Replies
Login or Register to Ask a Question