2

I've been looking everywhere. Didn't find the answer so I'm looking up to you!

Program in my job outputs many 10-50 *.pc files with folder-structure:

RESULTS/MODEL_Y0/Positioning_1.pc
RESULTS/MODEL_Y0/SK312_2SK_Y0_2012.pc

RESULTS/MODEL_Y100/Positioning_2.pc
RESULTS/MODEL_Y100/SK312_2SK_Y100_2012.pc

RESULTS/MODEL_Y250/Positioning_45.pc
RESULTS/MODEL_Y250/SK312_2SK_-Y575_2012.pc

Each PC file has inside absolute paths starting on xxx (not always 101, but it's always the second occurance of the INCLU word) line like:

INCLU / Positioning_1.pc
INCLU / /ST/statika/AGP-Pedestrian_Ansa-Meta/SK312_2SK_xxx/SK312_SERIE.inc
INCLU / /ST/statika/AGP-Pedestrian_Ansa-Meta/SK312_2SK_xxx/SK312_xPL_impactor.inc
INCLU / /ST/statika/AGP-Pedestrian_Ansa-Meta/SK312_2SK_xxx/SK312_materials.inc

I need to change these lines of absolute path to relative like:

INCLU / Positioning_1.pc
INCLU / ../../SK312_SERIE.inc
INCLU / ../../SK312_xPL_impactor.inc
INCLU / ../../SK312_materials.inc

Which I've done by writing a script placed above RESULTS folder: (part of the script)

grep -rl "${SEARCH}" --include \*.pc ./ | xargs sed -i "s#${SEARCH}#${REPLACE}#g"

where:

$SEARCH = /ST/statika/AGP-Pedestrian_Ansa-Meta/SK312_2SK_xxx/
$REPLACE = ../../

BUT here is the problem. When operating from longer than 81 chars path, the program will output the .pc files in the same folder-structure pattern, but inside the PC file, the absolute paths are separated to new line by - on 81. char position:

INCLU / Positioning_1.pc
INCLU / /ST/statika/uziv/JVERNER/PROJEKTY/Ansa/AGP-Pedestrian_Ansa-Meta/SK312_2S-
K_xxx/SK312_SERIE.inc
INCLU / /ST/statika/uziv/JVERNER/PROJEKTY/Ansa/AGP-Pedestrian_Ansa-Meta/SK312_2S-
K_xxx/SK312_xPL_impactor.inc
INCLU / /ST/statika/uziv/JVERNER/PROJEKTY/Ansa/AGP-Pedestrian_Ansa-Meta/SK312_2S-
K_xxx/SK312_materials.inc

where:

$SEARCH = /ST/statika/uziv/JVERNER/PROJEKTY/Ansa/AGP-Pedestrian_Ansa-Meta/SK312_2SK_xxx/

Here's the problem. My script doesn't see the variable $SEARCH. What's even bigger problem is that the path could be longer than 180 chars so there will be three-line path with two - dividers.

I can't comprehend how to write a script that would be functional with these multi-lines so that the path variable would shorten to ../../SK312_*.inc as before with short absolute one-line path.

  • Are the line numbers in your actual file? – terdon Oct 19 '15 at 17:38
  • Ah, sorry. No they aren't. I wanted to express how the linebreak works. First line starts with INCLU, second after the - at random position of the absolute path after the 81th minus "INCLU / " so after 73th char of the path. – Son Goku ssj4 Oct 19 '15 at 17:47
  • Ah, OK. Please edit your question and remove them since they are confusing. Could you also show us one of the problem paths? None of the paths you show are more than 81 characters long. Do you mean lines that are more than 81 characters? I also don't understand your output. You said that lines are cut in the output of your command but the cut lines you show still have the original path, not the modified one. Finally, please also tell us your operating system since this sort of thing can depend on the versions and implemetations of the standard tools you are using. – terdon Oct 19 '15 at 17:48
  • Line-numbers removed. The problem paths are there. :-) /ST/statika/uziv/JVERNER/PROJEKTY/Ansa/AGP-Pedestrian_Ansa-Meta/SK312_2SK_xxx/SK312_SERIE.inc was split to 2 lines: /ST/statika/uziv/JVERNER/PROJEKTY/Ansa/AGP-Pedestrian_Ansa-Meta/SK312_2S- & K_xxx/SK312_SERIE.inc In the PC file the 81 char line is: INCLU / /ST/statika/uziv/JVERNER/PROJEKTY/Ansa/AGP-Pedestrian_Ansa-Meta/SK312_2S- (with the INCLU / ) – Son Goku ssj4 Oct 19 '15 at 18:02
  • The program I use is outputting two kind of PC files relative on the working directory. 1st kind - with lines less than 81 chars long: INCLU / /ST/statika/AGP-Pedestrian_Ansa-Meta/SK312_2SK_xxx/SK312_SERIE.inc and the 2nd kind - with lines longer than 81 chars long separated into two lines and divided by - on the 81st char of the first line. I have to use Linux: OpenSuse 12.2 and every tool that is installed by default. My script has to work on all computers in my company. – Son Goku ssj4 Oct 19 '15 at 18:09
0

If your program is cutting the lines, you will need to join them before running your sed. For example:

grep -rl "${SEARCH}" --include \*.pc ./ | 
    xargs sed -i "s/-$//; s/-\n//; s#${SEARCH}#${REPLACE}#g"

Or

grep -rl "${SEARCH}" --include \*.pc ./ |
    xargs perl -i -pe "s/-\n//; s#$SEARCH#$REPLACE#g"

Alternatively, you could use find instead of grep:

find  -type f -name '*.pc' -exec perl -i -pe "s/-\n//; s#$SEARCH#$REPLACE#g" {} +

All of the above approaches will recurse into subdirectories.

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.