1

In the input file shown below, I am trying to find the string job_type and move the it to the next line if it matches job_type.

I tried this, but it is not working:

sed "s/[A-Z][a-z]*job_type:/\njob_type:/g"

Input:

    insert_job: VAU_vaultnotification_ertgvfg_job job_type: xxx 
    insert_job: VAU_vaultnotification_ertgvfg_frd job_type: yyy 
    insert_job: VAU_vaultnotification_ertgvfg_erb job_type: SXC 
     job_type: CMD
    insert_job: VAU_vaultnotification_ertgvfg_frd job_type: YUI 

Expected output:

insert_job: VAU_vaultnotification_ertgvfg_job 
job_type: xxx 
insert_job: VAU_vaultnotification_ertgvfg_frd 
job_type: yyy 
insert_job: VAU_vaultnotification_ertgvfg_erb 
job_type: SXC 
 job_type: CMD
insert_job: VAU_vaultnotification_ertgvfg_frd 
job_type: YUI 
1
  • 1
    I tried to clarify, but I don't understand what you mean by "and job_type trailed with alphabets and space". Should abcdjob_type be a match? Can you include some cases that should not be changed? How does the sed command fail? Does it not run? Does it run but do the wrong thing? Please edit your question and clarify.
    – terdon
    May 12 at 13:05

4 Answers 4

2

Your requirements are not clear to me, but maybe you want:

sed 's/\([[:upper:]][[:alpha:]_]*\)[[:space:]]\{1,\}\(job_type:\)/\1\
\2/g'

Or:

perl -Mopen=locale -pe 's/\b\p{Lu}\w*\K\s+(job_type:)/\n$1/g'
1

I would do something similar to this, using GNU sed:

sed '/insert_job/s/job_type/\njob_type/'

The first part /insert_job/ matches that on a line before doing the substitution. If "insert_job" (or whatever regex you prefer) is not found, it won't do the the substitution.

New contributor
James Casey is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
0

Using GNU sed

$ sed '/ \+\(insert_job.*\)\(job_type:.*\)/s//\1\n\2/' input_file
insert_job: VAU_vaultnotification_ertgvfg_job
job_type: xxx
insert_job: VAU_vaultnotification_ertgvfg_frd
job_type: yyy
insert_job: VAU_vaultnotification_ertgvfg_erb
job_type: SXC
     job_type: CMD
insert_job: VAU_vaultnotification_ertgvfg_frd
job_type: YUI
0

Using Raku (formerly known as Perl_6)

Assuming lines-to-be-modified all contain at least 4 whitespace-separated "columns" (lines with fewer than 4 "columns" are returned unmodified):

raku -ne 'my @a = .words; @a.elems >= 4 ?? put @a[0..1], "\n", @a[2..*] !! put @a;' 

Above assumes no string pattern in the input, only the appropriate number of whitespace-separated words (i.e. columns). For matching lines, the first 2 columns are returned, then a newline, then the remaining columns (the OP provides Sample Input wherein the string job_type begins the 3rd column).

However, if you want to use a regex and search for string patterns, Raku has your back (using the subst operator). Below, Raku searches for 8 regex atoms between / ^ ... $ /, between the ^ start-of-string and $ end-of-string zero-width assertions (anchors). Once matched, Raku's <( capture marker is used to drop everything before job_type from the match. Then in the subst replacement half, a \n newline is ~ concatenated to the $/ match-variable and returned.

raku -ne '.subst(/^ insert_job\: \s .+? \s <(job_type\: \s \w* \s*? $/, {"\n"~$/} ).put;'

Sample Input:

insert_job: VAU_vaultnotification_ertgvfg_job job_type: xxx 
insert_job: VAU_vaultnotification_ertgvfg_frd job_type: yyy 
insert_job: VAU_vaultnotification_ertgvfg_erb job_type: SXC 
 job_type: CMD
insert_job: VAU_vaultnotification_ertgvfg_frd job_type: YUI

Sample Output (first code example, above):

insert_job: VAU_vaultnotification_ertgvfg_job
job_type: xxx
insert_job: VAU_vaultnotification_ertgvfg_frd
job_type: yyy
insert_job: VAU_vaultnotification_ertgvfg_erb
job_type: SXC
job_type: CMD
insert_job: VAU_vaultnotification_ertgvfg_frd
job_type: YUI

Note, the OP hasn't made clear if all output should be left-justified or not (zero leading-whitespace characters). The first answer above is left-justified, however it's easy to trim or even indent all lines that start with job_type in the output (for example, in the second answer using {"\n "~$/} in the replacement will indent).

https://raku.org

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.