Extracting Multiple String in a File


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Extracting Multiple String in a File
# 1  
Old 08-07-2015
Extracting Multiple String in a File

Hello...

Request for help and assistance...

I have a file.txt that contains multiple lines similar from below and I would like to extract to other file in a tab/space delimiter some certain values or field.

Code:
/dir1/File1.File2_File3/content/data/File4.File5.Service:        <connection URL="jdbc:oracle:thin:@//hello.sample.com:9999/DBName" name="text1" password="xx123xx" username="forumID" validationQuery="test2 * text3 text4"/>


I would expect to execute a 'cat' command of the file.txt then do a PIPE '|' and may need to use a sed/cut/awk command to extract multiple strings in a file.


OUTPUT:
Code:
File1 File2 File3 File4.File5 hello 9999 DBName xx123xx forumID


Highly appreciate any help.

Thanks.

Last edited by WPGPoseidon; 08-07-2015 at 03:23 PM.. Reason: Add CODE tags.
# 2  
Old 08-07-2015
Could you please state, in English, the logic determining which words are to be discarded and which words are to be printed?

It isn't at all clear to me why your desired output excludes "world", "Default", "select", "from", and "dual" nor why "UNIXid" is included.
This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 08-07-2015
Hi Don,

Thank you for the response.
The below String is what I am expecting to be printed and the rest will be discarded.

Output:
Code:
File1 File2 File3 hello 9999 UNIXid xx123xx forumID


I'd just made the below lines as generic string.

Code:
/dir1/dir2/dir3/File1.File2_File3:        <connection URL="jdbc:oracle:thin:@//hello.world.com:9999/UNIXid" name="Default" password="xx123xx" username="forumID" validationQuery="select * from dual"/>

Moderator's Comments:
Mod Comment Please use CODE tags as required for forum rules when displaying sample input, output, and code segments.

Last edited by Don Cragun; 08-07-2015 at 02:35 PM.. Reason: Add CODE tags again.
# 4  
Old 08-07-2015
Quote:
Originally Posted by WPGPoseidon
Hi Don,

Thank you for the response.
The below String is what I am expecting to be printed and the rest will be discarded.

Output:
Code:
File1 File2 File3 hello 9999 UNIXid xx123xx forumID


I'd just made the below lines as generic string.

Code:
/dir1/dir2/dir3/File1.File2_File3:        <connection URL="jdbc:oracle:thin:@//hello.world.com:9999/UNIXid" name="Default" password="xx123xx" username="forumID" validationQuery="select * from dual"/>

Moderator's Comments:
Mod Comment Please use CODE tags as required for forum rules when displaying sample input, output, and code segments.
If there is no logic to describe what characters are to be included or excluded, here is the command you want to produce the output you want from an arbitrary input string:
Code:
echo 'File1 File2 File3 hello 9999 UNIXid xx123xx forumID'

# 5  
Old 08-07-2015
Quote:
Originally Posted by Don Cragun
If there is no logic to describe what characters are to be included or excluded, here is the command you want to produce the output you want from an arbitrary input string:
Code:
echo 'File1 File2 File3 hello 9999 UNIXid xx123xx forumID'


Hi Don,
That content is in a filename.txt that I need to 'cat' and so I will be expecting to get an ouput to be printed with those string.
# 6  
Old 08-07-2015
I repeat, if there are no rules describing what is to be extracted from a file and what is to be ignored in a file other than specifying the output you want, echo will give you what you want. If using cat to produce your output is important, you can slow down the echo and get the same results with:
Code:
cat filename.txt | echo 'File1 File2 File3 hello 9999 UNIXid xx123xx forumID'

# 7  
Old 08-07-2015
Hi Don,
There are multiple files or 1 file of text file that contains different entries for each line that I will expect to grab the string/field for each line.

Code:
LINE 1: /dir1/File1.File2_File3/content/data/File4.File5.Service:        <connection jdbcURL="jdbc:Oracle:thin:@//hello.sample.com:1234/ACCTID" name="text1" password="xx123xx" username="forumID" validationQuery="text2 * from dual"/>
LINE 2: /dir1/File7.File8_File9/content/data/Value1.Value2.Service:        <connection jdbcURL="jdbc:Oracle:thin:@//test.sample.com:3333/IBMID" name="text1" password="xx777zz" username="IDpass" validationQuery="text2 * from dual"/>

OUTPUT
Example of the Output
Code:
File1 File2 File3 File4.File5 hello 1234 ACCTID xx123xx forumID
File7 File8 File9 Value1.Value2 test 3333 IBMID xx777zz IDpass

I would expect to execute a 'cat' command of the file.txt then do a PIPE '|' and may need to use a sed/cut/awk command to extract multiple strings in a file.

Thank you.

Last edited by Don Cragun; 08-07-2015 at 04:09 PM.. Reason: Add CODE tags again.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extracting Multiple Lines from a Text File

Hello. I am sorry if this is a common question but through all my searching, I haven't found an answer which matches what I want to do. I am looking for a sed command that will parse through a large text file and extract lines that start with specific words (which are repeated throughout the... (4 Replies)
Discussion started by: MrDumbQuestion
4 Replies

2. Shell Programming and Scripting

replace (sed?) a string in file with multiple lines (string) from variable

Can someone tell me how I can do this? e.g: a=$(echo -e wert trewt ertert ertert ertert erttert erterte rterter tertertert ert) How do i replace the STRING with $a? I try this: sed -i 's/STRING/'"$a"'/g' filename.ext but this don' t work (2 Replies)
Discussion started by: jforce
2 Replies

3. Shell Programming and Scripting

Extracting string before first dash in a file name

Hi all, Given a file name such as EXAMPLE=lastname-02.30.71-firstname-town-other.tar.gz How do I print everything before the first dash (i.e. lastname) Note: I do not know exactly how many dashes or what information there will be in each file name so it is important that the code... (2 Replies)
Discussion started by: bashnewbee
2 Replies

4. Shell Programming and Scripting

replace (sed?) a single line/string in file with multiple lines (string) from another file??

Can someone tell me how I can do this? e.g: Say file1.txt contains: today is monday the 22 of NOVEMBER 2010 and file2.txt contains: the 11th month of How do i replace the word NOVEMBER with (5 Replies)
Discussion started by: tuathan
5 Replies

5. Shell Programming and Scripting

Extracting particular string in a file and storing matched string in output file

Hi , I have input file and i want to extract below strings <msisdn xmlns="">0492001956</ msisdn> => numaber inside brackets <resCode>3000</resCode> => 3000 needs to be extracted <resMessage>Request time getBalances_PSM.c(37): d out</resMessage></ns2:getBalancesResponse> => the word... (14 Replies)
Discussion started by: sushmab82
14 Replies

6. Shell Programming and Scripting

Extracting data between tags based on search string from unix file

Input file is on Linux box and the input file has data in just one line with 1699741696 characters. Sample Input: <xxx><document coll="uspatfull" version="0"><CMSdoc>xxxantivirus</CMSdoc><tag1>1</tag1></document><document coll="uspatfull"... (5 Replies)
Discussion started by: gaya
5 Replies

7. Shell Programming and Scripting

bash - batch script for extracting one file from multiple tar files

so i have hundreds of files named history.20071112.tar (history.YYYYMMDD.tar) and im looking to extract one file out of each archive called status_YYYYMMDDHH:MM.lis here is what i have so far: for FILE in `cat dirlist` do tar xvf $FILE ./status_* done dirlist is a text... (4 Replies)
Discussion started by: kuliksco
4 Replies

8. Shell Programming and Scripting

Extracting a string from one file and searching the same string in other files

Hi, Need to extract a string from one file and search the same in other files. Ex: I have file1 of hundred lines with no delimiters not even space. I have 3 more files. I should get 1 to 10 characters say substring from each line of file1 and search that string in rest of the files and get... (1 Reply)
Discussion started by: mohancrr
1 Replies

9. Shell Programming and Scripting

sed, grep, awk, regex -- extracting a matched substring from a file/string

Ok, I'm stumped and can't seem to find relevant info. (I'm not even sure, I might have asked something similar before.): I'm trying to use shell scripting/UNIX commands to extract URLs from a fairly large web page, with a view to ultimately wrapping this in PHP with exec() and including the... (2 Replies)
Discussion started by: ropers
2 Replies
Login or Register to Ask a Question