Separating string using linux command - linux

Suppose I have a folder containing files below formats:
hostname.anothername.ouut.ext
filename.another.anot.xxx
Now I want to separate the string that is inside of first dot and list in another file.
What should be the linux command? Here, hostname is just inside of the first name. My output will be in a separate file. Output format is given below:
hostname
filename
I only able to separated this words for file containing texts like hostname.xxxxx.yyyy,
filename.xxxx.tttt etc using
cut -d. -f1 <<END hostname.anothername.ouut.ext filename.another.anot.xxx END
But hostnae.xxxxx.yyyy, filename.uuuu.xxxxx etc are not text here these are file containing in a folder.

cut would be the simplest solution:
cut -d. -f1 <<END
hostname.anothername.ouut.ext
filename..another.anot.xxx
END
hostname
filename
for file in *; do
prefix=${file%%.*}
echo "$prefix"
done

Related

append a user specified suffix to the output of cat command

I would like to append a user specified suffix to the end of a file based on certain condition specified beforehand. I have the filenames stored in a file called changedfile.txt . I am executing the following command to get filename without any extension code.
cat changedfile.txt | cut -d "." -f1
I want to add a user provided suffix before the extension.
For example : If the output of the previous command was a/b/c.toml, and the user provided suffix is _backup, I want my final file to be renamed from a/b/c.toml to a/b/c_backup.toml. I have a for loop to handle the changing user suffix. I need a way to append the suffix to the file.
I thought something like this would work( thought += appends strings).
cat changedfile.txt | cut -d "." -f1 +backup
or
cat changedfile.txt | cut -d "." -f1 +=backup
got this error (cut: +backup: No such file or directory). I can understand why that command doesn't work.
Would appreciate if someone can get this working. For now even if there was a way to get it working for one suffix it's fine.I am using bash 3.2 .
cut just outputs the selected field, it can't make changes. You would need to pipe to some other tool to append something.
But you don't want to append, you want to insert in the middle of the line. You can do that entirely with sed.
sed 's/\./_backup./' changedfile.txt

How to use --file=script-file option to input a file to search and replace in sed command

I am doing a jenkins migration using jenkins-cli where in one step I am using sed command to replacing values manually as like below :
sed 's/mukesh/architect/g' target_file
But I would like to enter all the possible values in Input file with two column with delimeter as = and supply to target file
Input file looks like
ex:
mukesh=architect
abdul=manager
Now I want to use this file as input in sed command for search and replace in my target file. Instead of using s///g manually, I want to use the below option that I found in man page
-f script-file, --file=script-file
But not sure how to use this input file to auto search and replace the pattern in to the target file. It would be grateful if I get any samples, examples.
You can use below code to read input file, parse it and update outfile.
Here I am reading input file, separating values based on delimeter "=" and then updating outfile/target file.
while read name
do
x=`echo $name|cut -d"=" -f1`
y=`echo $name|cut -d"=" -f2`
sed -i "s/$x/$y/g" outfile
done < inputfile
This should solve your problem. Let me know if you are looking for something else or extra.cheers :)

Get numeric value from file name

I am a new guy of Linux. I have a question:
I have a bunch of files in a directory, like:
abc-188_1.out
abc-188_2.out
abc-188_3.out
how can a get the number 188 from those names?
Assuming (since you are on linux and are working with files), that you will use a shell / bash-script... (If you use something different (say, python, ...), the solution will, of course, be a different one.)
... this will work
for file in `ls *`; do out=`echo "${file//[!0-9]/ }"|xargs|cut -d' ' -f1`; echo $out; done
Explanation
The basic problem is to extract a number from a string in bash script (search stackoverflow for this, you will find dozens of different solutions).
This is done in the command above as (the string from which numbers are to be extracted being saved in the variable file):
${file//[!0-9]/ }
or, without spaces
${file//[!0-9]/}
It is complicated here by two things:
Do this recursively on the contents of a directory. This is done here with a bash for loop (note that the variable file takes as value the name of each of the files on the current working directory, one after another)
for file in ls *; do (commands you want done for every file in the CWD, seperated by ";"); done
There are multiple numbers in the filenames, you just want the first one.
Therefore, we leave the spaces in, and pipe the result (that being only numbers and spaces from the current file name) into two other commands, xargs (removes leading and trailing whitespace) and cut -d' ' -f1` (returns only the part of the string before the first remaining space, i.e. the first number in our filename),
We save the resulting string in a variable "out" and print it with echo $out,
out=echo "${file//[!0-9]/ }"|xargs|cut -d' ' -f1; echo $out
Note that the number is still in a string data type. You can transform it to integer if you want by using double brackets preceeded by $ out_int=$((out))

Grep: Copy a link with specific text

I have a text file with many links which aren't in separate lines.
I want to save in another file probably, all the links which contains a specific word.
How can I do this with grep?
EDIT:
To become more specifique, I have a messy txt file with many links. I want to copy in onother file all links starting with https:://, ending with .jpg and contains anywhere "10x10" string for example
You can get all the lines containing a specific word from the file like this:
LINKS=$(cat myfile.txt | grep MYWORD)
Then with LINKS, you can use a delimiter to create an array of links, which you can print to another file.
# Using a space as the delimeter
while IFS=' 'read -ra ind_link
do
echo $ind_link >> mynewfile.txt
done <<< "$LINKS"
Something along those lines I think is what you are looking for no?
Also if you need to refine your search, you can use the grep options such as -w to get more specific.
Hope it helps.
Could you give us the specific word and an example of input file ?
You could try to use egrep or/and sed like this (for example) :
egrep -o "href=\".*\.html\"" file|sed "s/\"\([^\"]*\)/\1/g"
Another exemple for all kind of http/https ressources links (whithout spaces in the URL) :
$ echo "<a href=http://titi/toto.jpg >"|egrep -o "https?:\/\/[^\ ]*"
http://titi/toto.jpg
$ echo "<a href=https://titi/toto.htm >"|egrep -o "https?:\/\/[^\ ]*"
https://titi/toto.htm
You have to customize the regexp according to your needs.

grep -f on files in a zipped folder

I am performing a recursive fgrep/grep -f search on a zipped up folder using the following command in one of my programs:
The command I am using:
grep -r -i -z -I -f /path/to/pattern/file /home/folder/TestZipFolder.zip
Inside the pattern file is the string "Dog" that I am trying to search for.
In the zipped up folder there are a number of text files containing the string "Dog".
The grep -f command successfully finds the text files containing the string "Dog" in 3 files inside the zipped up folder, but it prints the output all on one line and some strange characters appear at the end i.e PK (as shown below). And when I try and print the output to a file in my program other characters appear on the end such as ^B^T^#
Output from the grep -f command:
TestZipFolder/test.txtThis is a file containing the string DogPKtest1.txtDog, is found again in this file.PKTestZipFolder/another.txtDog is written in this file.PK
How would I get each of the files where the string "Dog" has been found to print on a new line so they are not all grouped together on one line like they are now?
Also where are the "PK" and other strange characters appearing from in the output and how do i prevent them from appearing?
Desired output
TestZipFolder/test.txt:This is a file containing the string Dog
TestZipFolder/test1.txt:Dog, is found again in this file
TestZipFolder/another.txt:Dog is written in this file
Something along these lines, whereby the user is able to see where the string can be found in the file (you actually get the output in this format if you run the grep command on a file that is not a zip file).
If you need a multiline output, better use zipgrep :
zipgrep -s "pattern" TestZipFolder.zip
the -s is to suppress error messages(optional). This command will print every matched lines along with the file name. If you want to remove the duplicate names, when more than one match is in a file, some other processing must be done using loops/grep or awk or sed.
Actually, zipgrep is a combination egrep and unzip. And its usage is as follows :
zipgrep [egrep_options] pattern file[.zip] [file(s) ...] [-x xfile(s) ...]
so you can pass any egrep options to it.

Resources