Extracting Multiple String in a File


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Extracting Multiple String in a File
# 8  
Old 08-07-2015
Would that do what you want?
Code:
perl -anF'[/._=":]' -e 'print "@F[2,3,4,7,8,17,20,21,27,30]\n"' file.txt


Last edited by Aia; 08-07-2015 at 04:54 PM..
This User Gave Thanks to Aia For This Post:
# 9  
Old 08-07-2015
Thank you so much Aia.

It works as expected.
# 10  
Old 08-07-2015
If you continue to refuse to use CODE tags as required by forum rules, you may be banned from this site! Please use CODE tags and stop depending on forum moderators to edit your posts for you.

Quote:
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.
There is never any need to use cat to feed input into sed, cut, or awk. All of those utilities are perfectly capable of reading files directly without using cat to unnecessarily read and write the contents of the files an extra time.

Please explain how you identify the string field in each of these lines. Sometimes it contains a period; usually it does not. Sometimes it contains part of the last pathname component of a pathname; sometimes it contains part of a directory name in that pathname as well; sometimes it doesn't. Sometimes your data starts with a pathname; sometimes it doesn't.

I'm very glad that Aia's suggestion is working for you. With the input you have provided in posts #1 and #7 (with the input from post #7 including the line number and with it not including the line numbers, since you didn't use CODE tags in your post and I couldn't tell whether or not that text was intended to be in the file):
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"/>
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"/>
/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"/>
/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"/>

I get the output:
Code:
dir2 dir3 File1         <connection URL  9999  Default forumID select * from dual
dir1 File1 File2 data File4  com 1234  
dir1 File7 File8 data Value1  com 3333  
File1 File2 File3 File4 File5 hello 1234 ACCTID xx123xx forumID
File7 File8 File9 Value1 Value2 test 3333 IBMID xx777zz IDpass

which does not match what you said you wanted as output from any of those lines:
Code:
File1 File2 File3 hello 9999 UNIXid xx123xx forumID
File1 File2 File3 File4.File5 hello 1234 ACCTID xx123xx forumID
File7 File8 File9 Value1.Value2 test 3333 IBMID xx777zz IDpass
File1 File2 File3 File4.File5 hello 1234 ACCTID xx123xx forumID
File7 File8 File9 Value1.Value2 test 3333 IBMID xx777zz IDpass

Where the text shown in red does not match what was requested and in some cases additional output was provided that you didn't says was what you wanted.

Showing us more seemingly random input and output without specifying what rules govern how the string field to be printed is determined is a waste of our time.

I wish you luck in solving your problem.
# 11  
Old 08-08-2015
Quote:
Originally Posted by WPGPoseidon
Thank you so much Aia.

It works as expected.
Thank you for the feedback, specially, since it was nothing more than an approximation of what I interpreted you were looking for. Nevertheless, if it would have not been, it could have been fixed by providing more feedback based on the output of it.
For me, it is never a waste of time.
# 12  
Old 08-08-2015
Quote:
Originally Posted by Aia
Thank you for the feedback, specially, since it was nothing more than an approximation of what I interpreted you were looking for. Nevertheless, if it would have not been, it could have been fixed by providing more feedback based on the output of it.
For me, it is never a waste of time.
Hi Aia,
If you look back at the earlier posts in this thread, you'll note that I provided code that did exactly what the OP requested and asked for details describing how we were supposed to determine what text was supposed to be extracted from an input file. The response so far has been that with x as input, y is the desired output.

You provided a great piece of code that didn't produce the output requested for any of the three sample input lines the OP posted. Apparently the OP has a solution that meets his or her requirements, but since we don't really know what those requirements are, I don't see how either of us can write code to meet those requirements.

- Don
# 13  
Old 08-08-2015
Hi Don,

People, at times, do not know how to define their ideas/requirements or effectively provide a coherent or accurate message, in these technical questions. Placing some "impersonal" pressure on them to provide as we demand, most likely, will produce an effect of discrepancy.
I volunteer my time to help the OP and what I get out of it is my own business. However, I found, in these cases, that providing an approximation of what I think they want, helps the process of identifying the real requirement and proper help, either by me or some else.

~ Aia
# 14  
Old 08-09-2015
Quote:
Originally Posted by Aia
I volunteer my time to help the OP and what I get out of it is my own business.
On one hand you are right: it is your time you spend and you are entitled to spend it any way you see fit. On the other hand please be aware that this board is a collaborative effort and we (that is: the moderation team) not only provide answers to questions but also to enforce certain politics in the process. Don's continuous asking about what the parameters of requested solutions exactly should be have - many times over and over - brought to light not only misconsceptions underlying in the questions but also revealed problems to be homework and coursework which are - for a reason - prohibited in the main forum.

This might not have been the case here but neither did you know nor did you undergo any effort to find out. I value your effort and your work here but i kindly ask you not to make the work for my colleagues any harder than it already is. We are volunteers like you.

Quote:
Originally Posted by Aia
Placing some "impersonal" pressure on them to provide as we demand, most likely, will produce an effect of discrepancy.
[...]
However, I found, in these cases, that providing an approximation of what I think they want, helps the process of identifying the real requirement and proper help, either by me or some else.
Let us talk about plastering: there are two ways to do it. The first one is to figure out the exact parameters of the wall, the plaster to be used, how the way the final wall is supposed to look like, etc. and then carefully apply the plaster in the right way. The second way is to throw plaster against the wall, wait until (that is the usual result) two thirds of it have fallen offf again, collect the remnants at the base, mix it with some water again, throw it again against the wall, rinse and repeat until finally a point is reached where the result borders on being satisfying.

One could argue that the second method produces at least some results immediately whereas the first method results in nothing being "really done" in the same time. One could argue that many people wanting to have a house built have no clear conception about what it should look like so the best the mason can do is to do build something and maybe tear it down again if it happens not to be what the principal wanted.

Still, as any mason can tell you, there is a sacred tradition among the craftsmen to only use the first method nevertheless. You might want to think about that.

bakunin
These 2 Users Gave Thanks to bakunin For This Post:
 
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