LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-08-2017, 08:23 PM   #1
ampsys
LQ Newbie
 
Registered: Jan 2014
Posts: 8

Rep: Reputation: Disabled
Isolate string between patters with sed


I have a file like this:
Code:
./Daily/SERVER1/SERVER1-20160101010101
./Daily/SERVER1/SERVER1-20160102010101
./Daily/SERVER1/SERVER1-20160103010101

I want to get at the SERVER1 part of that path (by itself), as well as the SERVER1-'Date' part by itself, and store each as variables.

Something like:
$SERVER=$(echo "line"|sed -e '|./Daily/||'|sed -e...
$SERVERDIR=$(...

I just can't figure out how to isolate these portions of the path...

Thanks for your help.
 
Old 03-08-2017, 08:43 PM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,336

Rep: Reputation: 4176Reputation: 4176Reputation: 4176Reputation: 4176Reputation: 4176Reputation: 4176Reputation: 4176Reputation: 4176Reputation: 4176Reputation: 4176Reputation: 4176
"Something like:" ain't going to get you anywhere. Especially with regex. Easiest is probably to precisely describe the bit you want and use back-references to only keep that. Each variable should be assignable with only one sed invocation.
Bash parameter substitution could handle the part with the date in easily enough.
 
Old 03-08-2017, 08:50 PM   #3
ampsys
LQ Newbie
 
Registered: Jan 2014
Posts: 8

Original Poster
Rep: Reputation: Disabled
Wow. That was helpful. Not.

I've tried 50 + ways of passing sed to parse it down to what I want but can't paste them all.

I'm asking for help because this is likely SUPER common for a bash scripter and that's not my regular thing and I'm trying to show my work.

Thanks for being a dikk about it, though.
 
Old 03-08-2017, 08:53 PM   #4
ampsys
LQ Newbie
 
Registered: Jan 2014
Posts: 8

Original Poster
Rep: Reputation: Disabled
Wow. That was helpful. Not.

I've tried 50 + ways of passing sed to parse it down to what I want but can't paste them all.

I'm asking for help because this is likely SUPER common for a bash scripter and that's not my regular thing and I'm trying to show my work.

Thanks for being a dikk about it, though.

I read the sed man. I'm trying to put in PRACTICE. Not get a lecture "ain't gonna cut it".
 
Old 03-08-2017, 11:57 PM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,426

Rep: Reputation: 2786Reputation: 2786Reputation: 2786Reputation: 2786Reputation: 2786Reputation: 2786Reputation: 2786Reputation: 2786Reputation: 2786Reputation: 2786Reputation: 2786
Well, if the file is like that, then (just as an alternative approach)
Code:
echo './Daily/SERVER1/SERVER1-20160103010101'|cut -d'-' -f2
20160103010101

echo './Daily/SERVER1/SERVER1-20160103010101'|cut -d'/' -f3
SERVER1
and you can concat the 2 results however you want.
 
Old 03-09-2017, 01:08 AM   #6
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,683
Blog Entries: 4

Rep: Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941
Quote:
Originally Posted by ampsys View Post
I have a file like this:
Code:
./Daily/SERVER1/SERVER1-20160101010101
./Daily/SERVER1/SERVER1-20160102010101
./Daily/SERVER1/SERVER1-20160103010101
I want to get at the SERVER1 part of that path (by itself), as well as the SERVER1-'Date' part by itself, and store each as variables.
I'm going to guess that you are trying a shell script and not perl or python.

In shell variables are assigned without a dollar sign, but need one all other times.

Code:
foo="123 456 789";
echo $foo;
echo foo;
So if you have that list of files in a separate file to be read one line at a time, you could do it like this using dirname and basename:

Code:
while read line; 
do 
        f=$(basename $line);
        d=$(dirname $line); 
        echo File = $f; 
        echo Directory = $d; 
done < files.txt
Be sure to see the manual pages for all the options. And see realpath if you need to calculate the full path.

Code:
man dirname
man basename
man realpath
read is a built-in function of the shell. See the manual page for your particular shell. Treat that manual page like a reference and don't be afraid to skip the irrelevant material and search down to the right section.

Code:
man bash
 
Old 03-09-2017, 08:45 AM   #7
allend
LQ 5k Club
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware64-15.0
Posts: 6,526

Rep: Reputation: 2812Reputation: 2812Reputation: 2812Reputation: 2812Reputation: 2812Reputation: 2812Reputation: 2812Reputation: 2812Reputation: 2812Reputation: 2812Reputation: 2812
Quote:
I want to get at the SERVER1 part of that path (by itself), as well as the SERVER1-'Date' part by itself, and store each as variables.
Why?
Quote:
Wow. That was helpful. Not.
Without background, how can anybody help?

If you just want a listing of server and date, then
Code:
awk -F [-/] '{print $4,$5}' <filename>
Redirect output to a file for a permanent record.

There are many ways to achieve your immediate goal. Without knowing the final goal, we are thrashing in the dark.
 
Old 03-09-2017, 09:17 AM   #8
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278

Rep: Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694
Here is a working model integrating everything said above:

Assuming your input file looks like so:
Code:
[root@tools ~]# cat input
./Daily/SERVER1/SERVER1-20160103010101
./Daily/SERVER1/SERVER1-20160103010102
./Daily/SERVER1/SERVER1-20160103010103
./Daily/SERVER1/SERVER1-20160103010104
This code:
Code:
while read -r line
do 
var1=$(echo $line | cut -d'/' -f3)
var2=$(echo $line | cut -d'/' -f4)
echo $var1
echo $var2
done < input
Will do what you are asking.
 
  


Reply


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Sed/awk/cut to pull a repeating string out of a longer string StupidNewbie Programming 4 09-13-2018 04:41 AM
how do i replace a text string in a file with a random string? (with sed etc) steve51184 Linux - Software 16 09-02-2010 12:05 PM
Trying to change String using sed with a string \/home\/user\/Desktop icecoolcorey Programming 10 06-13-2008 12:32 AM
How do I isolate a piece of string? vous Programming 4 03-16-2005 02:43 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:04 AM.

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