Swapping the 1st 4 lines only


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Swapping the 1st 4 lines only
# 1  
Old 06-17-2015
Swapping the 1st 4 lines only

How can you swap the first 4 line only, the rest will stay the same.
thanks


Code:
#!/bin/sh

line=4

awk -v var="$line" 'NR==var {
  s=$0
  getline;s=$0"\n"s
  getline;print;print s
  next
}1' fileko.tx

.


desired output:
Code:
this is line5
this is line6
this is line7
this is line8
this is line1
this is line2
this is line3
this is line4
this is line9
this is line10
this is line11

# 2  
Old 06-17-2015
Try
Code:
awk -vLNO=4 'NR<=LNO {TMP[NR]=$0;next} 1; NR==LNO*2 {for (i=1; i<=LNO; i++) print TMP[i]}' file
5
6
7
8
1
2
3
4
9
10

This User Gave Thanks to RudiC For This Post:
# 3  
Old 06-17-2015
Hello invinzin21,

Following may help you in same.
Code:
 awk '{if(NR==1 || NR==2 || NR==3 || NR==4){A=A?A ORS $0:$0;next};if(NR==8){print $0 ORS A;} else {print}}'  Input_file

Output will be as follows.
Code:
this is line5
this is line6
this is line7
this is line8
this is line1
this is line2
this is line3
this is line4
this is line9
this is line10
this is line11

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 4  
Old 06-17-2015
Quote:
Originally Posted by RudiC
Try
Code:
awk -vLNO=4 'NR<=LNO {TMP[NR]=$0;next} 1; NR==LNO*2 {for (i=1; i<=LNO; i++) print TMP[i]}' file
5
6
7
8
1
2
3
4
9
10

cat to explain? the syntax?

---------- Post updated at 11:01 PM ---------- Previous update was at 11:00 PM ----------

Quote:
Originally Posted by RavinderSingh13
Hello invinzin21,

Following may help you in same.
Code:
 awk '{if(NR==1 || NR==2 || NR==3 || NR==4){A=A?A ORS $0:$0;next};if(NR==8){print $0 ORS A;} else {print}}'  Input_file

Output will be as follows.
Code:
this is line5
this is line6
this is line7
this is line8
this is line1
this is line2
this is line3
this is line4
this is line9
this is line10
this is line11

Thanks,
R. Singh

cat to explain? the syntax? so next time i wont ask here.. cahrcter by character
# 5  
Old 06-17-2015
Code:
awk -vLNO=4 '                                   # supply desired line count into LNO variable
NR<=LNO         {TMP[NR]=$0;next}               # rows up to LNO stored in TMP array, don''t print
1                                               # default action: print
NR==LNO*2       {for (i=1; i<=LNO; i++)         # print the TMP array after another LNO rows (assumption, not specified)
                        print TMP[i]}
' file

# 6  
Old 06-17-2015
Hello invinzin21,

Following is explanation for same code, hope this will be helpful.
Code:
 awk '{if(NR==1 || NR==2 || NR==3 || NR==4){A=A?A ORS $0:$0;next};if(NR==8){print $0 ORS A;} else {print}}'  Input_file
 if(NR==1 || NR==2 || NR==3 || NR==4)  #### Looking for condition if line number is equal to 1 or 2 or 3 or 4
{A=A?A ORS $0:$0;next}                          #### If above condition is TRUE then I am creating a variable which will hold the 
 current line's value with it's previouls value character ? is to perform a action when condition before will be TRUE and : is used to perform action when condition is FALSE.
next                                                         #### After above variable's creation leave next statements and move next
 if(NR==8)                                               #### Looking for condition when line number is 8
{print $0 ORS A;}                                    #### if above condition is TRUE then print the 8th line and print variable A, which has now value of lines from 1 to 4 in it
else                                                         #### Apart of any line pther than 8 it will do simple print operation.

Thanks,
R. Singh

Last edited by RavinderSingh13; 06-17-2015 at 12:13 PM.. Reason: Sorry for aligning not sure why I am always not able to align text properly, may be copying from notepad, sorry for same.
# 7  
Old 06-17-2015
Hi.

The basic line editor ed has a "batch" mode and a "move" instruction:
Code:
#!/usr/bin/env bash

# @(#) s1	Demonstrate move lines with line editor, ed.

# Infrastructure: scaffolding, functions, debugging, identification, etc.
LC_ALL=C ; LANG=C ; export LC_ALL LANG
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C ed

FILE=${1-data1}
cp sacred $FILE

pl " Input data file $FILE:"
cat $FILE

pl " Results:"
ed $FILE 2>/dev/null <<EOF
1,4m8
w
q
EOF
cat $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 5.0.8 (lenny, workstation) 
bash GNU bash 3.2.39
ed GNU Ed 0.7

-----
 Input data file data1:
1
2
3
4
5
6
7
8
9
10

-----
 Results:
5
6
7
8
1
2
3
4
9
10

Best wishes ... 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

Swapping lines

Hi there, I have a text that I'm trying to format into something more readable. However, I'm stuck in the last step. I've searched and tried things over the internet with no avail. OS: Mac After parsing the original text that I won't put here, I managed to get something like this, but this... (8 Replies)
Discussion started by: Kibou
8 Replies

2. Solaris

Swapping

Hi Guys I am using SPARC-T4 (chipid 0, clock 2998 MHz), SunOS 5.10 Generic_150400-38 sun4v. How do I see if the server was doing some swapping like yesterday? I had a java application error with java.lang.OutOfMemoryError, now I want to check if the server was not doing some swapping at... (4 Replies)
Discussion started by: Phuti
4 Replies

3. UNIX for Dummies Questions & Answers

append following lines to 1st line, every 3 lines

I have output like this: USER_ID 12/31/69 19:00:00 12/31/69 19:00:00 USER_ID 12/31/69 19:00:00 12/31/69 19:00:00 USER_ID 12/31/69 19:00:00 12/31/69 19:00:00 USER_ID 12/31/69 19:00:00 12/31/69 19:00:00 ... where USER_ID is a unique user login followed by their login timestamp and... (6 Replies)
Discussion started by: MaindotC
6 Replies

4. Shell Programming and Scripting

AWK swapping fields on different lines

Hi All, Sorry if this question has been posted elsewhere, but I'm hoping someone can help me! Bit of an AWK newbie here, but I'm learning (slowly!) I'm trying to cobble a script together that will save me time (is there any other kind?), to swap two fields (one containing whitespace), with... (5 Replies)
Discussion started by: Bravestarr
5 Replies

5. Shell Programming and Scripting

need to delete all lines from a group of files except the 1st 2 lines

Hello, I have a group of text files with many lines in each file. I need to delete all the lines in each and only leave 2 lines in each file. (3 Replies)
Discussion started by: script_op2a
3 Replies

6. Shell Programming and Scripting

Swapping three lines

I have some text: <date>some_date</date> <text>some_text</text> <name>some_name<name> and I want to transform it to smthng like that: some_name on some_date: some_text I've tried sed: sed 's/<text>\(.*\)<\/text> <name>\(.*\)<\/name>/\2 - \1/' but it says unterminated... (13 Replies)
Discussion started by: dsjkvf
13 Replies

7. Shell Programming and Scripting

swapping lines that match a condition using sed, perl or the like

I'm a bit new to regex and sed/perl stuff, so I would like to ask for some advice. I have tried several variations of scripts I've found on the net, but can't seem to get them to work out just right. I have a file with the following information... # Host 1 host 45583 { filename... (4 Replies)
Discussion started by: TheBigAmbulance
4 Replies

8. Shell Programming and Scripting

Swapping lines beginning with certain words using sed/awk

I have a large file which reads like this: fixed-address 192.168.6.6 { hardware ethernet 00:22:64:5b:db:b1; host X; } fixed-address 192.168.6.7 { hardware ethernet 00:22:64:5b:db:b3; host Y; } fixed-address 192.168.6.8 { hardware ethernet 00:22:64:5b:db:b4; host A; }... (4 Replies)
Discussion started by: ksk
4 Replies

9. Shell Programming and Scripting

Swapping or switching 2 lines using sed

I made a script that can swap info on two lines using a combination of awk and sed, but was hoping to consolidate the script to make it run faster. If found this script, but can't seem to get it to work in a bash shell. I keep getting the error "Too many {'s". Any help here would be appreciated:... (38 Replies)
Discussion started by: LaTortuga
38 Replies

10. SuSE

Swapping

Hello! Why does my SuSE GNU/Linux machine swap? I have a Gig of ram, currently 14MBs of free RAM, 724MB - buffers and caches... That is 685MB of cached RAM, then kernel really should'nt have to swap, It should release cached memory in my thinkin... It has only swaped 3MB's but still,... (3 Replies)
Discussion started by: Esaia
3 Replies
Login or Register to Ask a Question