logo
down
shadow

Delete or Replace nth line in a file after a pattern match using shell scripts


Delete or Replace nth line in a file after a pattern match using shell scripts

By : user2955469
Date : November 22 2020, 10:40 AM
it fixes the issue I need to replace 11th line after the pattern match "msq_Query" in a file, the original text should remain intact. A one-liner sed or awk would be helpful !! , I think the easiest way to do this would be with awk like this:
code :
awk -v lines=11 'BEGIN { ignore = -1 } /pattern/ { ignore = NR + lines } NR != ignore { print }' filename
BEGIN { ignore = -1 }             # initialize ignore with -1 so NR will never
                                  # be equal to it by accident

/pattern/ { ignore = NR + lines } # when the pattern is found, set ignore to the
                                  # line we want to ignore

NR != ignore { print }            # unless the current line is that line, print
                                  # it.
awk -v lines=11 'BEGIN { mark = -1 } /pattern/ { mark = NR + lines } NR == mark { sub(/[0- 9]+/, "") } { print }' filename
BEGIN { mark = -1 }                # initialize mark with -1 so NR will never
                                   # be equal to it by accident

/pattern/ { mark = NR + lines }    # when the pattern is found, set mark to the
                                   # line we want to ignore

NR == mark { sub(/[0-9]+/, "0"); } # if that line is the current line, substitute
                                   # stuff (stuff can be freely chosen)

{ print }                          # print all the lines.


Share : facebook icon twitter icon
How to delete the last space at the end of each line in a text file? (shell scripts)

How to delete the last space at the end of each line in a text file? (shell scripts)


By : Sunil Hamne
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , I assume you want to delete trailing spaces and tabs from the end of each line.
code :
awk '{ sub(/[ \t]+$/, ""); print }' file
How to delete an specific line of a file using POSIX shell scripts?

How to delete an specific line of a file using POSIX shell scripts?


By : Emily
Date : March 29 2020, 07:55 AM
hope this fix your issue I am unsure about POSIX compliance, but can suggest using ed to delete the third line of a file as follows:
code :
echo $'3d\nwq' | ed -s YourFile
printf "3d\nwq\n" | ed -s YourFile
Insert/delete line to a file if pattern match

Insert/delete line to a file if pattern match


By : Naqi Chauhan
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further I think the crucial problem with your code is that it does a global replace and writes the output while it iterates through all lines.
This should produce the desired output
code :
users = %w/user1 user2 user3/
_var = users.map { |u| "(#{u})" }.join
_var = "(|#{_var})" if users.length > 1
f = File.open("ldap_config.conf").read

changed = 
  if f.match(/ldap_filter_users/)
    f.gsub!(/ldap_filter_users\s+=.*$\n/, '') 
  elsif f.match(/ldap_filters =/)
    f.gsub!(/ldap_filters = yes/, "ldap_filters = yes\nldap_filter_users = #{_var}")
  end

File.write("ldap_config.conf", f) if changed
awk search pattern in file. replace line on match and print as it is on not match

awk search pattern in file. replace line on match and print as it is on not match


By : WaermundDinasay
Date : March 29 2020, 07:55 AM
Any of those help 1 is a pattern with default action {print $0}
http://www.gnu.org/software/gawk/manual/html_node/Very-Simple.html
bash shell to add a new line in a file after a pattern match found

bash shell to add a new line in a file after a pattern match found


By : LearnWorkfront
Date : March 29 2020, 07:55 AM
will help you You properly use double-quotes in echo "${Array[1]}", and the braces {} needed for the array index. But you neglect to do so in your sed line. So try:
Related Posts Related Posts :
  • Security of su root in bash script
  • how to replace a string at a specific position in a csv file using bash
  • how to delete duplicate lines in a text file in unix bash?
  • Compare a variables in bash
  • How do I use sed on a string?
  • Using bash variables for range in sed
  • convert a DURATION HH:MM:SS to seconds in bash
  • Shell make system paths click-able when printed out by my shell script
  • I want to use parallel-ssh to run a bash script on multiple servers, but it simple prints the echo statements
  • AWK between 2 patterns - first occurence
  • Nginx on docker - daemonize and attach to bash
  • Actual return code for SCP
  • How to exit a bash function that expects a return
  • Bash script to split file into two files while changing output file during processing
  • Pass command line arguments via sbatch
  • variable substitution (what do you call/name this method)?
  • Call function from another shell script
  • How to Grep and Replace With Exclusions
  • shell command to remove characters after a special character in bash/shell
  • How do I run a script for 1st working day of every month in cron?
  • Bash Unexpected End of File Error After Installing RVM
  • Nested Loop with increment on inner loop?
  • video orientation detection in bash
  • Interrupt sleep in bash with a signal trap
  • Want file rename script to add number at end with fixed length
  • What's the DRYest way to replace a file with a symlink in bash
  • How to run Bitcoin's autogen.sh file?
  • Renaming files in folder and subfolders using a list- Bash Script/Unix
  • Bash command to compare more than two files
  • If It is a directory append text
  • What's wrong with my WHILE-DO loop, or why my script can't find the file I'm giving it on a plate?
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org