I've been told I can concatenate files in a sort of batch file by using sh.
e.g.
cat file1 file2 >> file3
cat file1 file4 >> file5
Is it possible to do a string replacement, I mean load file, replace x with y and run the file with sh.
The 'sed' utility, the stream editor, is the shell tool to do this. You could do something like the following to do what you asked, join file1 and file2, change all occurances of x to y and then run the commands with the bash shell:
cat file1 file2 | sed -e 's/x/y/g' | bash
Related
I want to combine multiple text files into one text file. Is there any command in ubuntu terminal to do this, or do I have to do it manually?
Try cat like
cat file1 file2 file3 > outputFile
cat stands for concatenation.
> is for output redirection.
If outputFile already has something in it and you wish to append to it the contents of other files, use
cat file1 file2 file3 >> outputFile
as > would erase the old contents of outputFile if it already existed.
Have a look here as well.
The scenario is that i have 2 files which i want to diff side by side using the following command with the line numbers:
diff -y file1.txt file2.txt
and
sdiff file1.txt file2.txt
The above command just prints the side by side diff but doesn't display the line numbers. Is there any way to do it ? I searched a lot but couldn't find any solutions. I can't use third party tools FYI. Any genius ideas from anyone ?
Update:
I want the file numbers present of the file itself and not the line numbers generated by piping to cat -n etc.. Lets say, i am doing diff using "--suppress-common-lines" then the line numbers should be omitted which are not shown in the diff.
Below code can be used to display the uncommon fields in two files, side by side.
sdiff -l file1 file2 | cat -n | grep -v -e '($'
Below code will display common fields along with line numbers in the output.
diff -y file1 file2 | cat -n | grep -v -e '($'
sdiff -s <(cat -n file1.txt) <(cat -n file2.txt)
This gives you side-by-side output with line-numbers from the source files.
The following command will display the side-by-side output prepended with line numbers for file1.txt and identical lines removed.
sdiff -l file1.txt file2.txt | cat -n | grep -v -e '($'
I had the same issue and ended up using a graphical tool (diffuse) under fedora 28
I am using CentOS. I have a file that contains information like:
100000,UniqueName1
100000,UniqueName2
100000,UniqueName4
100000,SoloName9
I want to split this out into files, one for each line, each named:
[secondvalue]_file.txt
For an example:
SoloName9_file.txt
Is it possible to split the file in this fashion using a command, or will I need to write a shell script? If the former, what's the command?
Thank you!
Here's one approach. Use the sed command to turn this file into a valid shell script that you can then execute.
sed -e 's/^/echo /g' -e 's/,/ >/g' -e 's/$/_file.txt/g' <your.textfile >your.sh
chmod +x your.sh
./your.sh
Note that trailing whitespace in the file would take some additional work.
Writing it into a shell script file gives you a chance to review it, but you can also execute it as a single line.
sed -e 's/^/echo /g' -e 's/,/ >/g' -e 's/$/_file.txt/g' <your.textfile | sh
I'm a very new user to bash, so bear with me.
I'm trying to run a bash script that will take inputs from the command line, and then run a c program which its output to other c programs.
For example, at command line I would enter as follows:
$ ./script.sh -flag file1 < file2
The within the script I would have:
./c_program -flag file1 < file2 | other_c_program
The problem is, -flag, file1 and file2 need to be variable.
Now I know that for -flag and file this is fairly simple- I can do
FLAG=$1
FILE1=$2
./c_program $FLAG FILE1
My problem is: is there a way to assign a variable within the script to file2?
EDIT:
It's a requirement of the program that the script is called as
$ ./script.sh -flag file1 < file2
You can simply run ./c_program exactly as you have it, and it will inherit stdin from the parent script. It will read from wherever its parent process is reading from.
FLAG=$1
FILE1=$2
./c_program "$FLAG" "$FILE1" | other_c_program # `< file2' is implicit
Also, it's a good idea to quote variable expansions. That way if $FILE1 contains whitespace or other tricky characters the script will still work.
There is no simple way to do what you are asking. This is because when you run this:
$ ./script.sh -flag file1 < file2
The shell which interprets the command will open file2 for reading and pipe its contents to script.sh. Your script will never know what the file's name was, therefore cannot store that name as a variable. However, you could invoke your script this way:
$ ./script.sh -flag file1 file2
Then it is quite straightforward--you already know how to get file1, and file2 is the same.
I have one file (for example: test.txt), this file contains some lines and for example one line is: abcd=11
But it can be for example: abcd=12
Number is different but abcd= is the same in all case, so could anybody give me command for finding this line and remove it?
I have tried: sed -e \"/$abcd=/d\" /test.txt >/test.txt but it removes all lines from my file and I also have tried: sed -e \"/$abcd=/d\" /test.txt >/testNew.txt but it doesn't delete line from test.txt, it only creates new file (testNew.txt) and in this file it removes my line. But it is not what I want.
Based on your description in your text, here is a cleaned-up version of your sed script that should work.
Assuming a linux GNU sed
sed -i '/abcd=/d' /test.txt
If you're using OS-X, then you need
sed -i "" '/abcd=/d' /test.txt
If these don't work, then use old-school sed with a conditional mv to manage your tmpfiles.
sed '/abcd=/d' /test.txt > test.txt.$$ && /bin/mv test.txt.$$ test.txt
Notes:
Not sure why you're doing \"/$abcd=/d\", you don't need to escape " chars unless you're doing more with this code than you indicate (like using eval). Just write it as "/$abcd=/d".
Normally you don't need '-e'
If you really want to use '$abcd, then you need to give it a value AND as you're matching the string 'abcd=', then you can do
abcd='abcd='
sed -i "/${abcd}/d" /test.txt
I hope this helps.
Here's a solution using grep:
$ grep -v '^\$abcd=' test.txt
Proof of concept:
$ cat test.txt
a
b
ab
ac
$abcd=1
$abcd=2
$abcd
ab
a
$abcd=3
x
$ grep -v '^\$abcd=' test.txt
a
b
ab
ac
$abcd
ab
a
x
As far as I know, this command can be used to create some other file with the deleted lines. Now that we have another file we can rename that file and delete the original file if we want.
You will just have to do this
grep -v '^\$abcd=' test.txt > tmp.txt
now tmp.txt will have contents
a
b
ab
ac
$abcd
ab
a
x
If you want you may rename this to test.txt after deleting test.txt