moving text within file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting moving text within file
# 1  
Old 08-13-2009
moving text within file

I want to mvoe lines around in a file.
Say I have 30 lines in a file and want to move the last 5 lines to the top of the file..how can this be done?
i thought of awk and sed but was not sure context.
please assist
thanks
# 2  
Old 08-13-2009
Code:
awk ' FNR < 6 {arr[FNR]=$0}
        FNR > 5 { print $0}
        END {for(i=1;i<6;i++) {print arr[i]} } ' filename

One way.
# 3  
Old 08-14-2009
line_move
Code:
if [ $# -ne 4];then
echo "Usage: line_move file_to_be_moved start_line_number end_line_number new_position"
fi
file=$1
start=$2
end=$3
position=$4
sed -n "${start},${end}p" ${file} > ${file}.tmp
sed -e "${start},${end}d" -e "${position}r ${file}.tmp" ${file}

# 4  
Old 08-14-2009
no good...neither code worked...

here is the contents of the file
Code:
15.5
15.8
16.05
16.29
16.14
16.25
16.34
16.4
15.9
15.3
14.7
14.2
13.3
12.6
12.1
11.7
11.3
11.5
11.7
11.9
23.4
15.5
15.5
15.5

# 5  
Old 08-14-2009
Code:
nawk '
{ a[FNR]=$0}
END {
  for(i=FNR-4;i<=FNR;i++)
    print a[i]
  for(i=1;i<=FNR-5;i++)
    print a[i]
}' myFile

# 6  
Old 08-14-2009
Hi.

The interactive line editor ed can be driven by a script. Using a data file of the NATA alphabet (line numbers added for the display):
Code:
#!/usr/bin/env bash

# @(#) s1	Demonstrate moving data inside file with editor "ed".

echo
set +o nounset
LC_ALL=C ; LANG=C ; export LC_ALL LANG
echo "Environment: LC_ALL = $LC_ALL, LANG = $LANG"
echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version "=o" $(_eat $0 $1) edges ed
set -o nounset
echo

FILE=${1-data1}
cp ${FILE}.orig $FILE

echo " Data file $FILE top and bottom:"
edges -l 3 $FILE

echo
echo " Results:"
ed $FILE <<'EOF'
$-4,$m0
w
q
EOF
edges $FILE

exit 0

Producing:
Code:
% ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian GNU/Linux 5.0 
GNU bash 3.2.39
edges (local) 307
GNU Ed 0.7

 Data file data1 top and bottom:
     1	Alpha
     2	Bravo
     3	Charlie
   ...
    24	Xray
    25	Yankee
    26	Zulu

 Results:
164
164
     1	Victor
     2	Whiskey
     3	Xray
     4	Yankee
     5	Zulu
   ...
    22	Quebec
    23	Romeo
    24	Sierra
    25	Tango
    26	Uniform

See man ed for details ... cheers, drl
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Match text to lines in a file, iterate backwards until text or text substring matches, print to file

hi all, trying this using shell/bash with sed/awk/grep I have two files, one containing one column, the other containing multiple columns (comma delimited). file1.txt abc12345 def12345 ghi54321 ... file2.txt abc1,text1,texta abc,text2,textb def123,text3,textc gh,text4,textd... (6 Replies)
Discussion started by: shogun1970
6 Replies

2. UNIX for Dummies Questions & Answers

Help! With File Moving

Hello, This is my first post, so please forgive my obvious lack of UNIX knowledge. I am trying/needing to write a script that follows this functional flow: 1. Access a config file that contains format:<directory> <filetype> <daterange> <directory> <filetype> <daterange> <directory>... (2 Replies)
Discussion started by: WildBeard83
2 Replies

3. UNIX for Advanced & Expert Users

Need help on moving .csv file from UNIX to windows file path

Need help on moving .csv file from unix to windows file path. (1 Reply)
Discussion started by: lakshmanraok117
1 Replies

4. Shell Programming and Scripting

moving file

Hello ALL, i hope everyone is fine here. I have found some directories that have 777 permission with below command. find ./ -type d -perm 0777 e/uploads/ e/uploads/s1 j/uploads/ j/uploads/s1 I want that if there is any php|html|css file found in above directory so move those... (4 Replies)
Discussion started by: learnbash
4 Replies

5. Shell Programming and Scripting

Need help in finding filesize , moving file , gzipping file

Hi , Please help with the following questions 1) how can i find size of a file ? i have written du -k $flname > s1 . Is this right ? Any other better suggeastions ? 2) how do I use mv command for moving the file ? I need the syntax with some examples 3) Command for printing the total... (1 Reply)
Discussion started by: Learning!
1 Replies

6. Shell Programming and Scripting

moving text with sed

Hi, I have a configuration file for solaris zones that I generate as part of another script but I need to move part of the text to the end of the file prior to execution and I'm having problems getting the syntax correct on the sed command. The file looks like this: create -b set... (7 Replies)
Discussion started by: wibbles
7 Replies

7. Shell Programming and Scripting

Moving a part of the text in a file

*************** #some other text ***************** *************** #some other text ***************** address1=1.1.1.1 address2=2.2.2.2 address3=3.3.3.3 I have a file where i need to push all the text starting from address1 till end of file to, below . Can anyone of you... (6 Replies)
Discussion started by: srikanthgoodboy
6 Replies

8. Shell Programming and Scripting

Finding & Moving Oldest File by Parsing/Sorting Date Info in File Names

I'm trying to write a script that will look in an /exports folder for the oldest export file and move it to a /staging folder. "Oldest" in this case is actually determined by date information embedded in the file names themselves. Also, the script should only move a file from /exports to... (6 Replies)
Discussion started by: nikosey
6 Replies

9. Shell Programming and Scripting

sh: Inserting tabs and moving text to 1 line

I trying to extract certain text from a csv file and then placing it into another csv file, but having problems getting the data to placed in one line with tab separated fields. Basically would like to have text sent to interfaces.csv in one line seperated by tabs. As it currently places files... (6 Replies)
Discussion started by: 00000008
6 Replies

10. Shell Programming and Scripting

Moving part of Text in a file

Hi, I have this text in a file where I need to move part of the text.... <Relation1 OriginatingObjectID="Holding_1" RelatedObjectID="Party_1" id="Relation_1"> <OriginatingObjectType tc="4">Holding</OriginatingObjectType> <RelatedObjectType tc="6">Party</RelatedObjectType>... (4 Replies)
Discussion started by: mgirinath
4 Replies
Login or Register to Ask a Question