Linux - GeneralThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I have a file that has one line in it, and looks like this:
Code:
-a test.example.com -a next.example.com -a last.example.com
I'm trying to code a bash script that will cut the file up, and only give me the *.example.com portions. I imagine I'll need the cut command (or multiple cut commands). Something like this:
Code:
list=`cat thefile | cut -d"-"
I know that's not a working example, but I wanted to see if anyone else had attempted something similar before and could give me an example. The number of entries can be variable and the domain names can of course be anything, but it will always be in the same format and on one line.
Thanks in advance!
Hmm. That gets me running in the right direction I think. I do not know the number of entries in the file, but I can play and get that from "echo $@" I think.
Thanks!
I would use 'sed' to find all the lines containing the "-a" parameters then substitute the "-a" with nothing
Code:
sed -n -e 's/-a//p' file.txt
that's 'sed' '-n' (print only addressed lines specified with p) '-e'(xpression) 's'(ubstitute) '-a' with '' p(rint) for file.txt
there is also a way to use two expressions to first find a particular type of line (maybe it always starts with the same command) then process only those lines with the substitution you want.
I would use 'sed' to find all the lines containing the "-a" parameters then substitute the "-a" with nothing
Code:
sed -n -e 's/-a//p' file.txt
you forgot the "g" modifier. also, in OP's case, if he wants to use each example.com individually, then sed is not the correct tool for this purpose. A more "flexible" approach is to bring each example.com into an array of some sort so that each example.com may be used individually. Eg maybe OP wants to ping each host etc etc.
# split on string, not just single char
$var1 = "-a test.example.com -a next.example.com -a last.example.com";
@arr = split(/-a /, $var1);
print "@arr\n";
test.example.com next.example.com last.example.com
@ghostdog74, as you have it, awk would need some kind of internal loop, not to mention it would print a blank line as the first option. AND print every line, as is, before and after that. Also... the ideal solution would be to find the most generalized script that will work on any file with a '-a' formatted line. At the moment, we don't even know if the line starts with a command or if it follows a line that has a '\'.
#!/bin/bash
while read line
do
array=($line)
for ((i=1; i<${#array[*]}; i=i+2))
do
name="${array[$i]}"
suffix="${name#*.}"
echo "DEBUG: suffix is '$suffix'"
done
done < input.txt
@ghostdog74, as you have it, awk would need some kind of internal loop, not to mention it would print a blank line as the first option.
that's not a problem at all. Can be easily fixed. The crux is, whether OP wants to use each domain individually. If he wants to use each domain found individually, he can always grab them from the arrays/fields. Doing only a substitution serves nothing, except if OP only wants to make output nice.
my point was that both of our solutions were incomplete and further education on the OP's end would be required to make it usable. That and sending my first solution (yes yes with the 'g' option) into a variable would allow further processing later in the script anyway. the -n prints only the lines with -a in it then it substitutes -a with nothing. Web addresses don't have spaces or return characters. The variable storing the output of sed can then be processed anyway imaginable. bash will treat both the return character and a space as white space. The only flaw in my solution is if there is something before and after on the same line with the '-a' but then you could do other tests to figure that out. So no. substituting doesn't only make the output nice. It's part of the process of an incomplete solution.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.