How to read a specific string from a file and save the whole line in another file in Bash Shell script - linux

I have a file which contains many lines. Each line contains strings and numbers. My script should read the file and when a specific word is matched, it should save the complete line. For example one of the line contains: Computera abc23, when the script should find the word Computera in any line it should save the whole line i.e Computera abc23. The line should be saved in file1.
Similarly another line in the file contains word machine1, when the script should see the word machine1 it should save that line in file1
Here is my code:
#!/bin/sh
f="home/c/file.txt"
n=$(cat "$f")
echo "${n} >| /home/c/file1.txt"
My code reads and saves all the text from file to file1 But I would like to save only those lines where a specific word is matched

Something similar to this:
cat file | grep [pattern] | awk '{ print $number-of-column }' >> file1
I recommend reading about both cut and awk and how you can use them with grep command.

Related

Linux command to copy file contents one word per line into a new file created from the same command

I need to copy the file contents one word per line into a new file created from the same command.
This is the file:
Never
gonna
give you up
Never
gonna
let you down
Never
gonna
turn around and desert you
Command already tried:
cat a6q4-input.txt >> a6q4-pt2.txt
This copies the file to newly created file a6q4-pt2.txt in exact format however does not changes the layout of the word content to one word per line.
Perl one-liner:
perl -lne 'print for split' a6q4-input.txt > a6q4-pt2.txt
For each line of the files given on the command line (-n), split it up into words based on whitespace with split, and print each one on its own line with print. -l adds a newline after each print (And strips them from each line when read).
You can use tr to replace spaces with newlines:
tr ' ' '\n' < input.txt > output.txt
Use xargs:
cat in_file | xargs -n1 > out_file
Or use a Perl one-liner:
perl -lane 'print for #F' in_file > out_file
The Perl one-liner uses these command line flags:
-e : Tells Perl to look for code in-line, instead of in a file.
-n : Loop over the input one line at a time, assigning it to $_ by default.
-l : Strip the input line separator ("\n" on *NIX by default) before executing the code in-line, and append it when printing.
-a : Split $_ into array #F on whitespace or on the regex specified in -F option.
SEE ALSO:
perldoc perlrun: how to execute the Perl interpreter: command line switches

Want to append records in two file using shell script

My first input file contains records name abc.txt:
abc#gmail.com
bscd#yahoo.co.in
abcd.21#gmail.com
1234#hotmail.com
My second file contains record name details.txt:
123456^atulsample^1203320
I want my final file having output to be Final.txt:
abc#gmail.com^123456^atulsample^1203320
bscd#yahoo.co.in^123456^atulsample^1203320
abcd.21#gmail.com^123456^atulsample^1203320
I have uses sed command but I am not getting my required output.
Kindly help as I don't have much knowledge in shell scripting.
try something like this;
#!/bin/bash
while read -r line
do
detail="$line"
sed '/^[ \t]*$/d' abc.txt | sed "s/$/^${detail}/" >> Final.txt
done < "details.txt"
this is to delete blank lines;
sed '/^[ \t]*$/d' abc.txt
this is to append from details.txt
sed "s/$/^${detail}/"

Bash: opening file which name in listed inside another file

I have a file that contains a list of file names to be opened later.
After I load lines (file names) to variables, for a reason unknown to me I cannot open it as a file later.
Here is a simplified example of what i'm trying to do:
Main file's contents:
first_file.txt
second_file.txt
Bash commands:
read line < $main_file # file with the list, received as an argument
echo $line # to check that correct filename has been read
cat $line # attempt to dump "first_file.txt" contents <- FAILS
cat first_file.txt # read "first_file.txt" contents manually
Execution esult:
first_file.txt
: No such file or directory
*** this is 1st file's contents ***
*** ....
So, cat first_file.txt works, $line contains "first_file.txt", but cat $line fails...
I obviously misunderstand something here, suggestions are welcomed!
Edit:
As requested, here is cat -v $main_file's output:
first_file.txt^M
second_file.txt^M
third_file.txt^M
^M
^M
The ^M characters are carriage returns (a.k.a. \r) and are often part of a Windows line ending. They don't show up when you echo them, but they are messing up your ability to open a file with the text having it at the end.
The best solution is to remove them from your "main file." You could use the dos2unix tool if you have it, or you could use GNU sed like sed -i -e 's/\s+$//g' $main_file to edit it in place and remove the extra white space (which includes ^M) from the end of each line.

how to print every second line using sed after a specific line

I have a big text file URL.txt. I want to print every second line on the terminal (without altering the content of the file) after line number 40 using sed command.
With GNU sed you can do
sed -n '40~2 p' file

Combine two text files in bash script

I have a text file (FILE_A.txt) with this content:
Content1
Content2
Content3
And other text file (FILE_B.txt) with this content:
A[#]
B[#]
C[#]
I would like to combine FILE_A.txt and FILE_B.txt in other file (FILE_C.txt) in this way:
A[Content1]
B[Content2]
C[Content3]
How could I make this using bash shell in linux (sed, cut, grep, etc)?
Here we go.
# awk 'NR==FNR{a[NR]=$0;next;} sub(/#/,a[FNR])' FILE_A.txt FILE_B.txt
A[Content1]
B[Content2]
C[Content3]
How does this work?
NR==FNR - causes the following statements to be run if the record number matches the FILE record number - that is, we are currently reading only the firs tfile.
{a[NR]=$0;next;} - Store values from the first file in an array.
sub(/#/,a[FNR]) - Once we're in the second file, substitute # for the matching value stored from the first file. Note that this isn't inside curly brackets, so it's being evaluated as a condition. If the sub() statement succeeds, the current line is printed.
Use paste and sed as follows:
$ paste File_B.txt File_A.txt | sed 's/#]\s*\(.*$\)/\1]/g'
A[Content1]
B[Content2]
C[Content3]
The following reads both files concurrently, one line at a time, and store the lines in $value and $template. We then use bash's variable substring replacement to replace # within $template with the contents of $value.
exec 6<"FILE_B.txt" # open file for reading and assign file descriptor 6
while read -r value; do # loop through FILE_A.txt, storing each line as $value
read -r template <&6 # read a line from FILE_B.txt, store as $template
echo ${template/\#/$value} # replace value into the template in place of `#`
done <"FILE_A.txt"
exec 6<&- # close input file descriptor 6

Resources