LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-29-2019, 05:55 PM   #1
bulgin
Member
 
Registered: Mar 2010
Posts: 74

Rep: Reputation: Disabled
Tweak to sed/bash script needed


Hello, see attached sample-data2.txt and bashy.sh script.

bashy.sh worked with other but very similar data to the attached sample-data.txt to enumerate the gene data enclosed in parenthesis.

Original data as $1:
./bashy.sh "Gene Code (4A - 4F) D2 fragment, D74F"

The data I modified for use with this script to enumerate addresses as $1:

./script.sh "1824 3rd st (1A - 1F) Brooklyn NY, 12550-4456"

and it worked!

However, now I need to:

rather than use $1 to bring in only one entry, I would like to bring in a txt with entries, such as sample-data2.txt

That's where I'm stuck.

(rename bashy.sh.txt to bashy.sh)
Attached Files
File Type: txt sample-data2.txt (331 Bytes, 46 views)
File Type: txt bashy.sh.txt (784 Bytes, 50 views)
 
Old 11-30-2019, 01:18 AM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,308
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
It's easier if you use [code] [/code] tags around the script and post it inline. It will get more views that way.

That said, if I understand the question, you can wrap what you have as a function and then call the funtion on each line of the input file. Here is one way:

Code:
#!/bin/sh

gc() {
        PART1=$(echo "$1" | sed 's/\(.*\)\s(.*/\1/')
        . . .
}

while read g; do
        gc "$g"
done < $1
 
Old 11-30-2019, 02:23 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Please show the desired output from sample-data2.txt
 
Old 11-30-2019, 03:17 PM   #4
rnturn
Senior Member
 
Registered: Jan 2003
Location: Illinois (SW Chicago 'burbs)
Distribution: openSUSE, Raspbian, Slackware. Previous: MacOS, Red Hat, Coherent, Consensys SVR4.2, Tru64, Solaris
Posts: 2,803

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
Thumbs up

Quote:
Originally Posted by bulgin View Post
However, now I need to:

rather than use $1 to bring in only one entry, I would like to bring in a txt with entries, such as sample-data2.txt

That's where I'm stuck.

(rename bashy.sh.txt to bashy.sh)
If I'm interpreting your post correctly (which is questionable given the amount of cold medication I've been downing), what I would do is:

Take everything from "PART1=$(echo "$1" ..." to "done" and turn it into a bash function:
Code:
function parse_record() {

   PART1=$(echo "$1" ...

   ...

   done
}
Another assumption: You got that code block doing what you need it to do. (It sounded like that was working OK in your OP.)

Then, at the bottom of your script, use a loop like:

Code:
INPUTFILE=$1
cat $INPUTFILE | while read RECORD
do
   if [ -n "${RECORD} ]; then     # Process only non-empty records though we could
      parse_record "${RECORD}"    # get fooled by records with nothing but whitespace.
   fi
done
To further tweak the main script to handle multiple files on the command line, wrap the following around the above code block:

Code:
FILELIST=$*
for INPUTFILE in ${FILELIST}
do
   if [ -r ${INPUTFILE} ]; then    # File exists and we can read it.

      <see code block above minus the INPUTFILE=$1 bit>

   fi
done
Hoping this gets you close to what you're looking for.

Cheers...
 
  


Reply

Tags
bash scripting, sed bash



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] SED Guru Needed -- Combine Two SED Commands Into One Liner RandyTech Linux - Newbie 4 03-23-2017 08:03 AM
LXer: How to Tweak And Perfect The Unity Desktop With Unity Tweak Tool Linux/Ubuntu LXer Syndicated Linux News 0 04-04-2013 08:51 PM
Is this BASH tweak possible? (Formatting the output) Rob00 Programming 6 10-19-2009 12:43 AM
ICE window manager tweak help needed Old_Fogie Slackware 7 04-02-2007 04:16 PM
bash script with grep and sed: sed getting filenames from grep odysseus.lost Programming 1 07-17-2006 11:36 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:32 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
Open Source Consulting | Domain Registration