bash$ cat file1.txt file2.txt file3.txt file4.txt > ./mergedfile.txt
If any of the file*.txt does not have proper LF/EOF termination, I will get lesser line count.
Are there any failsafe approaches to fixed the LF/EOF issue?
Avoiding to the following results (assuming text with 2-liner in running numbers 1 2 in each line, and one of them had a malformed LF termination).
1
2
1
21
2
I am answering my own question and hope to get better alternatives.
instead of:
cat file1.txt file2.txt file3.txt file4.txt > ./mergedfile.txt
I was told it would be better with the following:
awk 1 file1.txt file2.txt file3.txt file4.txt > ./mergedfile.txt
Related
Suppose I have two files file1.txt and file2.txt. I need to extract all lines starting with patna from file1 and append it to file2.
file1.txt
patnabihar 11
delhidelhi 22
delhipatna
file2.txt
patnapatna
delhidelhi
output files should be
file1.txt
delhidelhi 22
delhipatna
file2.txt
patnapatna
delhidelhi
patnabihar 11
It needs to be appended to an existing file not create a new file.
Use awk to filter the lines started with "patna" and then append to file2.txt. Delete the lines started with "patna" in file1.txt.
$ awk '/^patna/' file1.txt >> file2.txt
$ sed -i '/^patna/d' file1.txt
I want to copy lines 10 to 15 of a file into another file in Unix.
I am having files file1.txt and file2.txt.
I want to copy lines 10 to 15 from file1.txt to file2.txt.
Open a terminal with a shell then
sed -n '10,15p' file1.txt > file2.txt
Simple & easy.
If you want to append to the end instead of wiping file2.txt, use >> for redirection.
sed -n '10,15p' file1.txt >> file2.txt
^^
AWK is also a powerful command line text manipulator:
awk 'NR>=10 && NR<=15' file1.txt > file2.txt
In complement to the previous answer, you can use one of the following 3 solutions.
sed
Print only the lines in the range and redirect it to the output file
sed -n '10,15p' file1.txt > file2.txt
head/tail combination
Use head and tail to cut the file and to get only the range you need before redirecting the output to a file
head -n 15 file1.txt | tail -n 6 > file2.txt
awk
Print only the lines in the range and redirect it to the output file
awk 'NR>=10 && NR<=15' file1.txt > file2.txt
I need to append a file in a specific location of another file.
I got the line number so, my file is:
file1.txt:
I
am
Cookie
While the second one is
file2.txt:
a
black
dog
named
So, after the solution, file1.txt should be like
I
am
a
black
dog
named
Cookie
The solution should be compatible with the presence of characters like " and / in both files.
Any tool is ok as long as it's native (I mean, no new software installation).
Another option apart from what RavinderSingh13 suggested using sed:
To add the text of file2.txt into file1.txt after a specific line:
sed -i '2 r file2.txt' file1.txt
Output:
I
am
a
black
dog
named
Cookie
Further to add the file after a matched pattern:
sed -i '/^YourPattern/ r file2.txt' file1.txt
Could you please try following and let me know if this helps you.
awk 'FNR==3{system("cat file2.txt")} 1' file1.txt
Output will be as follows.
I
am
a
black
dog
named
Cookie
Explanation: Checking here if line number is 3 while reading Input_file named file1.txt, if yes then using system utility of awk which will help us to call shell's commands, then I am printing the file2.txt with use of cat command. Then mentioning 1 will be printing all the lines from file1.txt. Thus we could concatenate lines from file2.txt into file1.txt.
How about
head -2 file1 && cat file2 && tail -1 file1
You can count the number of lines to decide head and tail parameters in file1 using
wc -l file1
Both files has lines of string and numeric data minimum of 2000 lines.
How to add non duplicate data from file2.txt to file1.txt.
Basically file2 has the new data lines but we also want to ensure we are not adding duplicate lines to file1.txt.
File1.txt > this is the main data file
File2.txt > this file has the new data we want to add to file1
thanks,
Sort the two files together with the -u option to remove duplicates.
sort -u File1.txt File2.txt > NewFile.txt && mv NewFile.txt File1.txt
Another option if the file is sorted, just to have some choice (and I like comm :) )
comm --check-order --output-delimiter='' -13 File1.txt File2.txt >> File1.txt
use awk:
awk '!a[$0]++' File1.txt File2.txt
You can use grep, like this:
# grep those lines from file2 which are not in file1
grep -vFf file1 file2 > new_file2
# append the results to file1
cat new_file2 >> file1
I want to use linux diff command to get the following output:
2,4c2,4
I only want to know the line numbers where the files are different. I don't want the actual line on the console.
Eg:
If I will execute the following command:
diff file1.txt file2.txt
I would like the following output:
2,4c2,4
I don't want the output:
2,4c2,4
< I need to run the laundry.
< I need to wash the dog.
< I need to get the car detailed.
---
> I need to do the laundry.
> I need to wash the car.
> I need to get the dog detailed.
I went through the manual of diff command but I wasn't able to find any option that would allow me to achieve what I want.
Pipe it to grep and only show lines beginning with numbers.
diff file1.txt file2.txt | grep '^[1-9]'
pass the flag -f .
-sh-4.1$ cat file1.txt
I need to run the laundry.
I need to wash the dog.
difdferen line
I need to get the car detailed.
-sh-4.1$ cat file2.txt
I need to run the laundry.
I need to wash the dog.
I need to get the car detailed.
-sh-4.1$ diff -f file1.txt file2.txt
d3
Edited as per #Barmar comment: for it to work on changed lines .. you can just filter lines with "<" or ">" by asking for the inverse of lines that start with "<" or ">"
First : plain diff :
-sh-4.1$ diff file*
3d2
< difdferen line
4a4
> different line in file2
-sh-4.1$
with grep to filter lines that start with < or >
-sh-4.1$ diff file* | egrep -v "^<|^> |^-"
3,4d2
5a4
3d2
4a4
simplified version suggested by #Barmar
-sh-4.1$ diff file1.txt file2.txt | egrep -v "^[-<>]"
3,4d2
5a4