Moving lines within a txt file


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
# 1  
Moving lines within a txt file

A newbie to shell scripting.....

I need some assistance in doing the following:

I have a system generated text file(a makefile basically).

Before I can execute the make command, I need to modify one section of this generated file.

The generated section is as follows:

# INCLUDE macro for SCI
#~~~~~~~~~~~~~~~~~~~~~~~
XYZ_MAIN_INCLUDE = \
-I/Path_1 \
-I/Path_2 \
-I/Path_3 \
-I/Path_4

Basically, Path_4 has be placed at the start of the INCLUDE section with appropriate changes to new line delimiters.

This modified section should look as follows:

# INCLUDE macro for SCI
#~~~~~~~~~~~~~~~~~~~~~~~
XYZ_MAIN_INCLUDE = \
-I/Path_4 \
-I/Path_1 \
-I/Path_2 \
-I/Path_3

I have to make similar changes to 6 different files in 6 different locations in the directory structure before I can execute make.

Can I achieve this using a shell script without having to modify all the files manually everytime I need to execute a make?

Thanks in advance for any suggestions.
# 2  
show exact sample of the make file if possible
# 3  
Thank you ghostdog74 for your response.

Not sure if I can post the contents of the entire makefile here:

The section that needs manual editing(with line numbers enabled) looks like this:

66 # INCLUDE macro for SCI
67 #~~~~~~~~~~~~~~~~~~~~~~~
68 CDIPOSIX_MAIN_INCLUDE = \
69 -I/dev/adapt_layer/s9cdi/CDIPosix/Posix_Main \
70 -I/dev/adapt_layer/s9cdi/CDIPosix/Posix_Main/inc \
71 -I/dev/adapt_layer/s9cdi/CDIInitialisation/Initialization \
72 -I/dev/adapt_layer/s9cdi/CDIPosix \
73 -I/dev/adapt_layer/s9cdi/CDIPosix/PCK_CDIPosix \
74 -I/gplus3/source/range/os20/include


The same section after manual editing looks like this:

66 # INCLUDE macro for SCI
67 #~~~~~~~~~~~~~~~~~~~~~~~
68 CDIPOSIX_MAIN_INCLUDE = \
69 -I/gplus3/source/range/os20/include \
70 -I/dev/adapt_layer/s9cdi/CDIPosix/Posix_Main \
71 -I/dev/adapt_layer/s9cdi/CDIPosix/Posix_Main/inc \
72 -I/dev/adapt_layer/s9cdi/CDIInitialisation/Initialization \
73 -I/dev/adapt_layer/s9cdi/CDIPosix \
74 -I/dev/adapt_layer/s9cdi/CDIPosix/PCK_CDIPosix

FYI, this section appears in between the same line numbers always. Does this information help?

Thanks
# 4  
Code:
awk 'FNR==NR{$1="";a[FNR]=$0;next}   
   FNR==4{print $1" "a[9];next}   
   FNR==9{print $1" " a[4]}
   FNR<9{print}   
' "file" "file"

output:
Code:
# ./test.sh
66 # INCLUDE macro for SCI
67 #~~~~~~~~~~~~~~~~~~~~~~~
68 CDIPOSIX_MAIN_INCLUDE = \
69  -I/gplus3/source/range/os20/include
70 -I/dev/adapt_layer/s9cdi/CDIPosix/Posix_Main/inc \
71 -I/dev/adapt_layer/s9cdi/CDIInitialisation/Initialization \
72 -I/dev/adapt_layer/s9cdi/CDIPosix \
73 -I/dev/adapt_layer/s9cdi/CDIPosix/PCK_CDIPosix \
74  -I/dev/adapt_layer/s9cdi/CDIPosix/Posix_Main \

not sure is it what you want.
# 5  
Thank you ghostdog74 for your reply.

Yes from the output this is what I want....but......

As I mentioned earlier, I have 6 files(a.mak, b.mak.....f.mak) in various locations in the directory structure, the contents of which needs to be similarly modified before I can invoke make.

Assuming I provide the location/paths of all 6 files somewhere in this script, will this script make modifications as above and save the files or is that too much to ask for?
# 6  
Quote:
Originally Posted by innocentspirit
Thank you ghostdog74 for your reply.

Yes from the output this is what I want....but......

As I mentioned earlier, I have 6 files(a.mak, b.mak.....f.mak) in various locations in the directory structure, the contents of which needs to be similarly modified before I can invoke make.

Assuming I provide the location/paths of all 6 files somewhere in this script, will this script make modifications as above and save the files or is that too much to ask for?
if you can do it on one file, you can do it on many files (of the same type). just need to do some scripting ..
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #644
Difficulty: Easy
The first web browser was called WorldWideWeb.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Join Lines every paragraph in a file.txt

Hi all, Is there any idea on how to automate convert the paragraph in one line in a file, this will happen after OCR the documents, OCR split every paragraph. I need to join all the paragraph in one line. #cat file.txtThe Commission on Higher Education (CHED) was created through Republic Act... (7 Replies)
Discussion started by: lxdorney
7 Replies

2. UNIX for Dummies Questions & Answers

Moving lines of file to command line

I have a file in which each line is the name of another file. Is there a way to serve them to the command line? For example, if the file contains file1.txt file2.txt file3.txt ... file9.txt is there a way to insert them in the command as a batch? If I ran a command like grep... (4 Replies)
Discussion started by: wbport
4 Replies

3. Shell Programming and Scripting

Getting lines from .txt file

Hi I have a file with contents: NAMES John carrey williams How can I get all the names and store them in seperate variables(or arrays) please keep in mind that the no. of such names is not known.Three here is a bogus value ~thanks (4 Replies)
Discussion started by: leghorn
4 Replies

4. UNIX for Dummies Questions & Answers

find lines in file1.txt not found in file2.txt memory problem

I have a diff command that does what I want but when comparing large text/log files, it uses up all the memory I have (sometimes over 8gig of memory) diff file1.txt file2.txt | grep '^<'| awk '{$1="";print $0}' | sed 's/^ *//' Is there a better more efficient way to find the lines in one file... (5 Replies)
Discussion started by: raptor25
5 Replies

5. Shell Programming and Scripting

merging two .txt files by alternating x lines from file 1 and y lines from file2

Hi everyone, I have two files (A and B) and want to combine them to one by always taking 10 rows from file A and subsequently 6 lines from file B. This process shall be repeated 40 times (file A = 400 lines; file B = 240 lines). Does anybody have an idea how to do that using perl, awk or sed?... (6 Replies)
Discussion started by: ink_LE
6 Replies

6. Shell Programming and Scripting

sed to cp lines x->y from 1.txt into lines a->b in file2.txt

I have one base file, and multiple target files-- each have uniform line structure so no need to use grep to find things-- can just define sections by line number. My question is quite simple-- can I use sed to copy a defined block of lines (say lines 5-10) from filename1.txt to overwrite an... (3 Replies)
Discussion started by: czar21
3 Replies

7. Shell Programming and Scripting

Deleting lines that contain spaces in a txt file

I need some help deleting lines in a file that contain spaces. Im sure awk or sed will work but i dont know much about those commands. Any help is appreciated :D (7 Replies)
Discussion started by: r04dw4rri0r
7 Replies

8. Shell Programming and Scripting

Urgent Need Help! Merging lines in .txt file

I need to write a script that reads through an input .txt file and replaces the end value with the end value of the next line for lines that have distance <=4000. The first label line is not actually in the input. In the below example, 3217 is the distance from the end of the first line to the... (12 Replies)
Discussion started by: awknerd
12 Replies

9. Shell Programming and Scripting

print all even lines of a txt file

In other news, I have a colors text file with hundreds of lines, and I want to print only the even numbered lines. for example I have this file looks something like this: ALLCOLORS.TXT red red green red blue red red red green red red blue green green green blue blue blue red blue blue blue... (1 Reply)
Discussion started by: ajp7701
1 Replies

10. Shell Programming and Scripting

how do I filter double lines from a txt file

Hi, I have a file with the following: access-list.txt router1 access-list 1 permit any any access-list 1 deny any any router2 access-list 2 permit any any access-list 2 deny any any router3 access-list 3 permit any any access-list 3 deny any any I want to hava an output that... (10 Replies)
Discussion started by: I-1
10 Replies

Featured Tech Videos