Replace first few lines with first few lines from other file
awk
is your friend
Script
# awk 'NR==FNR && FNR<=3 || NR>FNR && FNR>4' file2 file1
Output
1a
2a
3a
5
6
7
8
9
10
Tips
- NR - Total number of records processed
- FNR - Total number of records processed but resets when reading a new file.
- When a condition evaluates to true and no extra commands are given,
awk
just prints.
All good :-)
how to replace the first line of a file with the first line of another file
This might work for you (GNU sed):
sed -e '1R file1' -e '1d' file2
Read the first line of file2. Read the first line of file1 and insert it into the output, then delete the first line of file2. Now read and output the rest of file2.
replace a string from first line on multiple files
You are very close with your code, but you need to account for the trailing /
char after the .
char.
Assuming you are using a modern sed
with the -i
(inplace-edit) option you can do
sed -i '1s@supersonic\./@supersonic.com/@' *
Note that rather than have to escape /
inside of the s/srchpat\/withSlash/replaceStr/'
, you can use another char after the the s
command as the delimiter, here I use s@...@...@
. If your search pattern had a @
char, then you would have to use a different char.
Some older versions of sed
need to you to escape the alternate delimiter at the first use, so
sed 's\@srchStr@ReplStr@' file
for those cases.
If you're using a sed
that doesn't support the -i
options, then
you'll need to loop on your file, and manage the tmp files, i.e.
for f in *.html ; do
sed '1s@supersonic\./@supersonic.com/@' "$f" > /tmp/"$f".fix \
&& /bin/mv /tmp/"$f".fix "$f"
done
Warning
But as you're talking about 10,000+files, you'll want to do some testing before using either of these solutions. Copy a good random set of those files to /tmp/mySedTest/ dir and run one of these solutions there to make sure there are no surprises.
And you're likely to blow out the cmd-line MAX_SIZE with 10,000+ files, so read about find and xargs. There are many posts here about [sed] find xargs
. Check them out if needed.
IHTH
Replace next N lines before a first/second/third/... match including the match
Here's a BSD sed version (comment.sed):
/Text/, /}/ {
s|^|//|
/}/ {
:idle
n
b idle
}
}
The first line /Text/, /}/
is a regex address range. Every line within that that range (inclusive) will be commented out. At the end of this range, the second /}/
, we loop back to the idle
label consuming the rest of the file. Otherwise the address range would also comment out the second Text
block.
A one line version:
sed '/Text/, /}/ {s|^|//|; /}/ {:idle; n; b idle}}' input
To test:
diff expect <(sed -f comment.sed input) && echo ok
Find line in file and replace it with line from another file
Your sed
attempt contains several unexplained errors; it's actually hard to see what you are in fact trying to do.
You probably want to do something along the lines of
sed '3!d;s%.*%s/^matching\.string\.here.*/&/%' file2 |
sed -f - -i file1
It's unclear what you hope for the /s
to mean; does your sed
have a flag with this name?
This creates a sed
script from the third line of file2
; take out the pipeline to sed -f -
to see what the generated script looks like. (If your sed
does not allow you to pass in a script on standard input, you will have to write it to a temporary file, and then pass that to the second sed
.)
Anyway, this is probably both simpler and more robust using Awk.
awk 'NR==3 && NR==FNR { keep=$0; next }
/^matching\.string\.here/ { $0 = keep } 1' file2 file1
This writes the new content to standard output. If you have GNU Awk, you can explore its -i inplace
option; otherwise, you will need to write the result to a file, then move it back to file1
.
how to replace first line of a file using python or other tools?
Using Python 3:
import argparse
from sys import exit
from os.path import getsize
# collect command line arguments
parser = argparse.ArgumentParser()
parser.add_argument('-p', '--parameter', required=True, type=str)
parser.add_argument('-f', '--file', required=True, type=str)
args = parser.parse_args()
# check if file is empty
if getsize(args.file) == 0:
print('Error: %s is empty' % args.file)
exit(1)
# collect all lines first
lines = []
with open(args.file) as file:
first_line = next(file)
# Check if '=' sign exists in first line
if '=' not in first_line:
print('Error: first line is invalid')
exit(1)
# split first line and append new line
root, parameter = first_line.split('=')
lines.append('%s=%s\n' % (root, args.parameter))
# append rest of lines normally
for line in file:
lines.append(line)
# rewrite new lines back to file
with open(args.file, 'w') as out:
for line in lines:
out.write(line)
Which works as follows:
$ cat hanlp.properties
root=/Users/pan/Documents
other content
$ python3 script.py --file hanlp.properties --parameter /Users/a/b
$ cat hanlp.properties
root=/User/a/b
other content
Related Topics
Does Cron Expression in Unix/Linux Allow Specifying Exact Start and End Dates
How to Rebuild Rootfs in Buildroot
Force Gnu Linker to Generate 32 Bit Elf Executables
What Scheduling Algorithms Does Linux Kernel Use
Cpu Affinity Masks (Putting Threads on Different Cpus)
/Usr/Bin/Perl: Bad Interpreter: Text File Busy
Add Blank Line After Every Result in Grep
Component Based Web Project Directory Layout with Git and Symlinks
On X64 Linux, Differencebetween Syscall, Int 0X80 and Ret to Exit a Program
Compiling 32 Bit Assembler on 64 Bit Ubuntu
How to Set Environment Variables on Ec2 Instance via User Data
What Is Export_Symbol_Gpl in Linux Kernel Code
D-Bus Tutorial in C to Communicate with Wpa_Supplicant
How to Download the Torvalds Linux Kernel Master, (Re)Compile It, and Boot It with Qemu
Can 'Connect' Call on Socket Return Successfully Without Server Calling 'Accept'