Insert a newline after match in files of specific name under some subdirectories?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Insert a newline after match in files of specific name under some subdirectories?
# 1  
Old 10-30-2016
Insert a newline after match in files of specific name under some subdirectories?

Hi

I'd like to add the newline:

\tuser: nobody", or "<TAB>user: nobody

to all files named:

Code:
docker-compose.yml

in subfolders of pwd with names beginning with 10-20.

Within these files, I'd like to find the line (there'll only be one) containing:

Code:
command: celery worker

NOTE: As far as I know, each of these lines will actually contain:
command: celery worker -l info -A snakeeyes.blueprints.contact.tasks, but I haven't checked each file, some may differ so the above search pattern seemed reasonable.
And append on a new line directly below the matching line, the aforementioned string, either:

\tuser: nobody", or "<TAB>user: nobody

If anyone knows how to do this, a (if possible, verbosely annotated) answer would be greatly appreciated!

---
Not Important:

I've been trying to figure this out by myself - I had been reading Sed & Awk by Dougherty & Robbins earlier this year, but I haven't gotten very far, and there's nothing in my notes for this sort of case... here's what I had come up with thus far after searching line and combining different things I read:

Code:
find . -name docker-compose.yml -exec sed -i "command: celery worker/ a\\\tuser: nobody" {} \;

Mostly including this not as a starting point, but so you can laugh at it (it mangled the files)
Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by duncanbetts; 11-01-2016 at 01:25 PM.. Reason: Added CODE tags.
# 2  
Old 10-30-2016
Try
Code:
find . -name docker-compose.yml -exec sed -i "/command: celery worker/ a\\\tuser: nobody" {} \;

This User Gave Thanks to RudiC For This Post:
# 3  
Old 10-30-2016
Thanks RudiC, that did it Smilie.

Turned out I needed spaces for YAML.

Code:
find . -name docker-compose.yml -exec sed -i "/command: celery worker/ a\ \ \ \ user: nobody" {} \;

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash to move specific files to directory based on match to file

I am trying to mv each of the .vcf files in the variants folder to the folder in /home/cmccabe/f2 that the .vcf id is found in file. $2 in file will always have the id of a .vcf in the variants folder. The line in blue staring with R_2019 in file up to the -v5.6 will always be an exact match to a... (4 Replies)
Discussion started by: cmccabe
4 Replies

2. Shell Programming and Scripting

Insert newline when grep result is empty

Given a csv file with 40 columns with name, address, hometown etc. I use a bash command in 1 line which: 1. gets the address column and pipes that to 2. grep the first digit and everything that follows Command: awk -F ";" '{print $19}' /Users/jb/Desktop/ReorderTempTotal.csv | grep -o "\d.*"... (7 Replies)
Discussion started by: JBVeenstra
7 Replies

3. Shell Programming and Scripting

Insert a newline in XML

Hi guys, I got a requirement that ... (5 Replies)
Discussion started by: mohanalakshmi
5 Replies

4. UNIX for Dummies Questions & Answers

move files that match specific conditions

Hi all, I'm new to this forum and bash scripting. I have the following problem, I need to copy some files (from one dir. to another) whose first 5 numbers (subjects' ID) match the directory names. Here a shortened version of what I'm trying to do: names=(32983_f 35416_f 43579_f) # these are... (6 Replies)
Discussion started by: ada1983
6 Replies

5. Shell Programming and Scripting

sed insert text without newline

Hi, I use sed to insert text at beginning of a file. But sed inserts a newline after my text that I do not need. For example, I want to insert "foo" at the beginning of my file: > cat myfile This is first line. > sed -i '1i\foo' myfile > cat myfile foo This is first line. ... (5 Replies)
Discussion started by: tdw
5 Replies

6. Shell Programming and Scripting

Find files that do not match specific patterns

Hi all, I have been searching online to find the answer for getting a list of files that do not match certain criteria but have been unsuccessful. I have a directory that has many jpg files. What I need to do is get a list of the files that do not match both of the following patterns (I have... (21 Replies)
Discussion started by: nikos-koutax
21 Replies

7. Shell Programming and Scripting

AWK Script Issue insert newline for a regular expression match

Hi , I am having an issue with the Awk script to insert newline for a regular expression match Having a file like this FILE1 #################### RXOER , RXERA , RXERC , RXERD .RXEA(RXBSN), RXERD , REXCD input RXEGT buffer RXETRY ####################### Want to match the RXE... (38 Replies)
Discussion started by: jaita
38 Replies

8. Shell Programming and Scripting

Insert two newline after some pattern

Hi, I need to insert two newline characters after matching of a pattern in each line of a file. Eg. If i have a file with contents as follows:- Now, i want output as follows :- i.e., I need to insert two newline characters after the occurance of pattern "</Message>>". Thnx... (1 Reply)
Discussion started by: DTechBuddy
1 Replies

9. Shell Programming and Scripting

use regexp to insert newline within a line

I have successfully used regexp and sed to insert a newline before or after a line containing a matched pattern /WORD/. However, I want to insert a newline immediately following /WORD/ and not after the -line- containing the pattern matched. I can match a pattern, but it is matched via a wild card... (2 Replies)
Discussion started by: kpeirce
2 Replies

10. UNIX for Advanced & Expert Users

tar subdirectories for specific files

I have a sample directory structire like following # pwd /user/test and I have files like following out.txt A/a.txt B/b.txt C/c.txt (A,B,C are directories ) # tar cvf test.tar * a A/a.txt 1 blocks a B/b.txt 1 blocks a C/c.txt 1 blocks a out.txt 1 blocks But whenever I give (4 Replies)
Discussion started by: ronald_brayan
4 Replies
Login or Register to Ask a Question