LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 04-23-2014, 10:30 AM   #1
krmarshall87
LQ Newbie
 
Registered: Jul 2012
Posts: 8

Rep: Reputation: Disabled
Question Show only word based upon pattern - BASH


I would like to return the word(s)/string(s) based upon a pattern.

A grep -o returns only the pattern.
And a grep returns the entire line.
I'd like a middle ground.

Backstory:

User input into the script is a table of data. I have them input this via a "read -d, -a input", input being the array. (open to suggestions there too, but it is multi-row, thus newlines)

I currently just read first "cell" and then iterate until the next beginning cell, appending them all to a string.

Example user input:

server1 date time (static text string)
server2 date time (static text string)
server3 date time (static text string)
server4 date time (static text string)
, (to escape std input)

However, if the user copies in any additional data prior to what I perceive to be the beginning, then it would throw it off. While I could just parse the whole thing, my original question would simply the whole thing, as the server names fit a pattern- or set of patterns.
 
Old 04-23-2014, 11:26 AM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,577
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
A sed solution:
Code:
c@CW9:/tmp$ input='lorem ipso server foo fecit'
c@CW9:/tmp$ extract=$(echo "$input" | sed 's/^.*\(server [^ ]*\).*$/\1/')
c@CW9:/tmp$ echo "$extract"
server foo
 
Old 04-23-2014, 11:29 AM   #3
sag47
Senior Member
 
Registered: Sep 2009
Location: Orange County, CA
Distribution: Kubuntu x64, Raspbian, CentOS
Posts: 1,857
Blog Entries: 36

Rep: Reputation: 457Reputation: 457Reputation: 457Reputation: 457Reputation: 457
Why not use a more robust language that has proper data structures for handling tabular (and more complicated) data? Python, Ruby, Perl, C++, <insert other language>; take your pick. It sounds to me what you're doing is generally the hard way when there are better tools for the job. Use the right tool .
 
Old 04-23-2014, 12:05 PM   #4
krmarshall87
LQ Newbie
 
Registered: Jul 2012
Posts: 8

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by catkin View Post
A sed solution:
Code:
c@CW9:/tmp$ input='lorem ipso server foo fecit'
c@CW9:/tmp$ extract=$(echo "$input" | sed 's/^.*\(server [^ ]*\).*$/\1/')
c@CW9:/tmp$ echo "$extract"
server foo
This returns the server and next string. As you have, "server foo". I would only need server strings. When I tested with my array (and string- converted it to test it too), it doesn't strip out any strings- using a proper filter too. I think the problem may be that the multi-lines throw off the ^ and $ and the multiple lines is treated as a single string.

Quote:
Originally Posted by sag47
Why not use a more robust language that has proper data structures for handling tabular (and more complicated) data? Python, Ruby, Perl, C++, <insert other language>; take your pick. It sounds to me what you're doing is generally the hard way when there are better tools for the job. Use the right tool .
I am trying to disrupt the environment that I am in as little as possible. No new rpms, etc. So, I am using using shell.
 
Old 04-23-2014, 12:39 PM   #5
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,577
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Quote:
Originally Posted by krmarshall87 View Post
This returns the server and next string. As you have, "server foo". I would only need server strings. When I tested with my array (and string- converted it to test it too), it doesn't strip out any strings- using a proper filter too. I think the problem may be that the multi-lines throw off the ^ and $ and the multiple lines is treated as a single string.
The example sed command was just an example; without knowing what is a valid server string in your application is was impossible to be precise. Sed can work on multiple lines.

Can you give examples of the actual strings you want to validate? Sed is only one of the possible tools.
 
Old 04-23-2014, 12:53 PM   #6
krmarshall87
LQ Newbie
 
Registered: Jul 2012
Posts: 8

Original Poster
Rep: Reputation: Disabled
I was replacing with a valid filter.

Here is what the array or string would equal:

ondv267a 04/05/2014 04:53 Account password has not been reset from company default. onhq235b 04/11/2014 07:46 Account password has not been reset from company default. onhq2d08 04/10/2014 00:11 Account password has not been reset from company default. onhq2d09 04/10/2014 00:13 Account password has not been reset from company default. vm2227 04/06/2014 11:41 Account password has not been reset from company default. vm2228 04/06/2014 02:39 Account password has not been reset from company default. vm222d 04/09/2014 14:56 Account password has not been reset from company default. vm24a2 04/06/2014 15:10 Account password has not been reset from company default. vm281a 04/08/2014 18:38 Account password has not been reset from company default. vm289f 04/08/2014 08:19 Account password has not been reset from company default. vm28a4 04/08/2014 08:34 Account password has not been reset from company default. vm28a9 04/08/2014 07:39 Account password has not been reset from company default. vm28aa 04/08/2014 07:43 Account password has not been reset from company default. vm28ab 04/08/2014 07:47 Account password has not been reset from company default. vm28ac 04/08/2014 07:16 Account password has not been reset from company default. vm2c85 04/08/2014 12:53 Account password has not been reset from company default. vm2c86 04/08/2014 13:06 Account password has not been reset from company default. vm2c87 04/08/2014 13:53 Account password has not been reset from company default. vm2c88 04/08/2014 13:19 Account password has not been reset from company default. vm2c89 04/08/2014 13:13 Account password has not been reset from company default. vm2c8a 04/06/2014 18:25 Account password has not been reset from company default.

Coming from the table. Filter strings would be either onhq, ondv, or vm.

EDIT: Took it out of code tags.
 
Old 04-23-2014, 12:53 PM   #7
sag47
Senior Member
 
Registered: Sep 2009
Location: Orange County, CA
Distribution: Kubuntu x64, Raspbian, CentOS
Posts: 1,857
Blog Entries: 36

Rep: Reputation: 457Reputation: 457Reputation: 457Reputation: 457Reputation: 457
Quote:
Originally Posted by krmarshall87 View Post
I am trying to disrupt the environment that I am in as little as possible. No new rpms, etc. So, I am using using shell.
What environment? Many scripting languages come installed by default in server environments which are better suited to the task you're attempting than bash (don't get me wrong I love bash and use it for most things). e.g. my corporate uses RedHat Enterprise Linux and that comes with Python 2.7 installed by default as well as Perl. With your worry of disrupting the environment it sounds like you're working in a production environment that is actively in use by other users (e.g. web server or what have you). I highly recommend you working in a test environment as a rule of thumb so you can worry less about breaking the system and worry more about getting work done. By working in test you're afforded the ability to break things without worry and iron out your program bugs. Then when your program is production quality for users that's when you put the final stamp on it and move it to production.

I don't mean to preach. I'm just trying to give you sensible recommendations. I feel you're using the wrong tool for the job entirely.

Last edited by sag47; 04-23-2014 at 12:57 PM.
 
Old 04-23-2014, 09:55 PM   #8
allend
LQ 5k Club
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware-current
Posts: 5,052

Rep: Reputation: 1773Reputation: 1773Reputation: 1773Reputation: 1773Reputation: 1773Reputation: 1773Reputation: 1773Reputation: 1773Reputation: 1773Reputation: 1773Reputation: 1773
Why not use awk?
Code:
bash-4.2$ cat input.txt
ondv267a 04/05/2014 04:53 Account password has not been reset from company default. 
onhq235b 04/11/2014 07:46 Account password has not been reset from company default.
onhq2d08 04/10/2014 00:11 Account password has not been reset from company default. 
onhq2d09 04/10/2014 00:13 Account password has not been reset from company default. 
vm2227 04/06/2014 11:41 Account password has not been reset from company default.

bash-4.2$ awk '/^ond.*/ {print $1 $2 $3} /^onhq.*/ {print $3 $2 $1} /^vm.*/ {for ( i=4; i<=NF; i++ ) printf"%s ",$i;printf"%s", "\n"}' input.txt
ondv267a04/05/201404:53
07:4604/11/2014onhq235b
00:1104/10/2014onhq2d08
00:1304/10/2014onhq2d09
Account password has not been reset from company default. 
bash-4.2$
 
1 members found this post helpful.
Old 04-23-2014, 10:31 PM   #9
krmarshall87
LQ Newbie
 
Registered: Jul 2012
Posts: 8

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by allend View Post
Why not use awk?
Code:
bash-4.2$ cat input.txt
ondv267a 04/05/2014 04:53 Account password has not been reset from company default. 
onhq235b 04/11/2014 07:46 Account password has not been reset from company default.
onhq2d08 04/10/2014 00:11 Account password has not been reset from company default. 
onhq2d09 04/10/2014 00:13 Account password has not been reset from company default. 
vm2227 04/06/2014 11:41 Account password has not been reset from company default.

bash-4.2$ awk '/^ond.*/ {print $1 $2 $3} /^onhq.*/ {print $3 $2 $1} /^vm.*/ {for ( i=4; i<=NF; i++ ) printf"%s ",$i;printf"%s", "\n"}' input.txt
ondv267a04/05/201404:53
07:4604/11/2014onhq235b
00:1104/10/2014onhq2d08
00:1304/10/2014onhq2d09
Account password has not been reset from company default. 
bash-4.2$
Input is via the user and a read, thus a string or array; therefore, the beginning of the line method doesn't work. I just pulled up cygwin, wouldn't this have done it for a file:

Code:
awk '/^ondv.*/ {print $1} /^onhq.*/ {print $1} /^vm.*/ {print $1}' input.txt
Regardless, unless you have an idea for user input writing to file..
 
Old 04-23-2014, 11:28 PM   #10
allend
LQ 5k Club
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware-current
Posts: 5,052

Rep: Reputation: 1773Reputation: 1773Reputation: 1773Reputation: 1773Reputation: 1773Reputation: 1773Reputation: 1773Reputation: 1773Reputation: 1773Reputation: 1773Reputation: 1773
Why not have the user prepare the file using an editor and then post process the file?
 
Old 04-24-2014, 08:27 AM   #11
krmarshall87
LQ Newbie
 
Registered: Jul 2012
Posts: 8

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by allend View Post
Why not have the user prepare the file using an editor and then post process the file?
Keeping it simpler per user requirements. Not official user requirements, but I know the users..
 
Old 04-24-2014, 09:29 AM   #12
allend
LQ 5k Club
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware-current
Posts: 5,052

Rep: Reputation: 1773Reputation: 1773Reputation: 1773Reputation: 1773Reputation: 1773Reputation: 1773Reputation: 1773Reputation: 1773Reputation: 1773Reputation: 1773Reputation: 1773
If your users can use a keyboard, then learning to use nano would be straightforward. You are prompted for ^O to write the file then ^X to exit.
Code:
#!/bin/bash

nano input1.txt
# Your awk stuff goes here
 
Old 04-24-2014, 09:38 AM   #13
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,577
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
The choice of language is personal. Bash itself is not great at string handling but it can easily be combined with other tools such as sed and awk if performance is not an issue. If you want to use bash, that's your choice.

But, on the method of user input, I'm with allend. Even naive users would benefit from being able to copy and paste, correct errors etc. especially given the very repetitive nature of the input posted. Could the users prepare the input file using a simple GUI text editor, similar to MS Notepad?
 
Old 04-24-2014, 09:58 AM   #14
krmarshall87
LQ Newbie
 
Registered: Jul 2012
Posts: 8

Original Poster
Rep: Reputation: Disabled
I wouldn't be against using awk or sed that time is minimal compared to having users create a text file just to make that easier on me. I'd rather take the brunt of the work, increasing productivity down-the-line.

I may just use the following:

Code:
for word in ${input[@]}
do
	server=`echo $word|egrep -i '(vm|onhq|ondv|ondk)'`

	if [ "$server" ]
	then
		serverList+="$server,"
	fi
done
 
  


Reply

Tags
grep, input, table, user, word


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash script: using "select" to show multi-word options? (like "option 1"/"o zidane_tribal Programming 7 12-19-2015 02:03 AM
bash shell script read file word by word part 2 justina Programming 7 01-25-2011 02:19 PM
[SOLVED] bash shell script read file word by word. justina Programming 15 01-22-2011 11:12 AM
[SOLVED] /bin/bash if statement pattern search, end of pattern special character? headhunter_unit23 Programming 3 04-29-2010 09:05 AM
matching a pattern in a file & prefixing a word shivarajM Linux - Software 3 05-28-2009 02:14 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 06:51 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration