Sponsored Content
Top Forums Shell Programming and Scripting Append a line "while read line" Post 302982945 by Don Cragun on Wednesday 5th of October 2016 02:25:37 AM
Old 10-05-2016
Quote:
Originally Posted by fongthai
Hello everyone,

I want to process a data file using bash, I would like add a new line RIGHT after the time when i know "another variable met a value"

Like this:
Code:
while read line
do
 ...
 if [ $xyz == "ON" ]; then
 #Append a new line "===" to input.txt file after $line position
 #break
 fi
done < input.txt

That mean we don't know the position in advance, so I could not use "sed".
Any help please!
Thank you!
Quote:
Originally Posted by fongthai
input.txt

To make it easier, let assume xyz=2
So, I want look for the PIN line, then move it next xyz line (meaning: move it to next 2 lines)
The output should be like this:
Code:
line_before_pin_1
line_before_pin_2
..
line_after_pin_1
line_after_pin_2
PIN
line_after_pin_3
..
line_after_pin_end

Note that there are 2 actions:
- remove old PIN line
- add new PIN line to the right position
Let us be perfectly clear here. The requirements stated in post #1 in this thread and in post #5 in this thread are completely different. Adding a line containing === when $xyz expands to the string ON is in no way related to moving a line that contains only the string PIN 2 lines later in a file when $xyz expands to the string 2.

To move the first line in a file containing only the string PIN forward $xyz lines (if $xyz expands to a positive decimal value (with or without a leading +) or backwards $xyz lines (if $xyz expands to a negative decimal value), you could also try:
Code:
#!/bin/ksh
xyz=${1:-2}	# set default value for $xyz to 2, but use $1 if present.
if [ "${xyz#[+-]}" != "$xyz" ]
then	# $xyz is a signed value, clear $op
	op=
	# If $xyz is negative, decrement it by one to get the desired offset.
	[ "$xyz" -lt 0 ] && xyz=$((xyz - 1))
else	# $xyz is an unsigned value, set $op to a plus sign.
	op="+"
fi

# Use ed to move the first line that exactly matches the string "PIN" to the
# desired spot.
ed -s input.txt <<EOF
	H
	/^PIN$/;.m$op$xyz
	w
	q
EOF

This was written and tested using a Korn shell, but will work with any shell that uses Bourne shell syntax and performs the parameter and arithmetic expansions required by the POSIX standards.

If this script is invoked with no operands, with the operand 2, or the operand +2 it will move the 1st line in the file named input.txt that is exactly the string PIN two lines closer to the end of the file. If invoked with the operand -1, it will move that line one line closer to the start of the file. This will work with any non-zero decimal value as long as the that value is a valid for that line in the given file.

Note that the behavior of this script is slightly different from the script provided by RavinderSingh13:
  1. My script modifies the file input.txt in place. Ravinder's script copies the desired changes to standard output, but does not modify the file Input_file.
  2. My script only moves the 1st line that contains only the string PIN. Ravinder's script moves any line containing the string PIN. Although, if there are two or more lines containing PIN within $xyz lines of each other, only the 1st of those will be moved.
  3. If the specified offset is not valid in the input file, my script prints a diagnostic message and does not change the input file; Ravinder's script moves a line containing PIN to the end of the file.
  4. If there is no line containing PIN, Ravinder's script copies the input file to standard output unchanged; my script prints a diagnostic message and leaves the input file unmodified.
  5. Ravinder's script will move matched lines towards the end of the file; my script will move the 1st matched line forwards or backwards.
Both of our scripts meet your requirements as specified in post #5 in this thread. But, you left a lot of behavior unspecified that our scripts handle in different ways (as noted above).
This User Gave Thanks to Don Cragun For This Post:
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies

2. Shell Programming and Scripting

each line as 20digit long append zero "0" in front

i have an ip file like 121 1213412 34345353 long file want to made each line as 20digit long append zero "0" in front as 121 become 00000000000000000121 (1 Reply)
Discussion started by: RahulJoshi
1 Replies

3. Shell Programming and Scripting

sed append "\n" to end of every line in file

I know it sounds simple, but I want to e-mail the last 6 lines of a log file, which I have tailed into logresults.txt. I'm using echo -e "Subject:server results\nFrom:server log <user@domain.com>\n"`cat logresults.txt` | sendmail -t user@domain.com which works, but the body of the e-mail has... (4 Replies)
Discussion started by: unclecameron
4 Replies

4. Shell Programming and Scripting

script to read a line with spaces bet " " and write to a file

Hi, I need a command in UNIX KSH below is the description... MAPPING DESCRIPTION ="Test Mapping for the calid inputs" ISVALID ="YES" NAME ="m_test_xml" OBJECTVERSION ="1" VERSIONNUMBER ="1" unix ksh command to read the DESCRIPTION and write to a file Test Mapping for the calid inputs... (3 Replies)
Discussion started by: perlamohan
3 Replies

5. Shell Programming and Scripting

defining a variable using multiple "entries/columns" within a line using read

Hello All, I am trying to figure out how to use the read command. I have a txt file that has rows looking something like this: 1 2 3 4 5 6 7 8 9 where each number represents an entry of various characters deliminated by tabs. My goal is to set entries 2-7 as a variable/string that I... (3 Replies)
Discussion started by: Torinator
3 Replies

6. Shell Programming and Scripting

SED or AWK "append line to the previous line"

Hi, How can I remove the line beak in the following case if the line begin with the special char “;”? TEXT Text;text ;text Text;text;text I want to convert the text to: Text;text;text Text;text;text I have already tried to use... (31 Replies)
Discussion started by: research3
31 Replies

7. Shell Programming and Scripting

append "awk command" to the end of each line

hi; this is my qqq.mos: l ./gcsw 86.0.0.1 'lt all;l+;lset SectorPort=860 Tilt 861;l-' l ./gcsw 86.0.0.2 'lt all;l+;lset SectorPort=862 Tilt 863;l-' l ./gcsw 86.0.0.3 'lt all;l+;lset SectorPort=864 Tilt 865;l-' ... i want to append;l nawk 'NR==14 && $NF!="Set."{print "l ./gcsw "r"... (4 Replies)
Discussion started by: gc_sw
4 Replies

8. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

9. Shell Programming and Scripting

Move a line containg "char" above line containing "xchar"

Okay, so I have a rather large text file and will have to process many more and this will save me hours of work. I'm not very good at scripting, so bear with me please. Working on Linux RHEL I've been able to filter and edit and clean up using sed, but I have a problem with moving lines. ... (9 Replies)
Discussion started by: rex007can
9 Replies

10. Shell Programming and Scripting

Failure: if grep "$Var" "$line" inside while read line loop

Hi everybody, I am new at Unix/Bourne shell scripting and with my youngest experiences, I will not become very old with it :o My code: #!/bin/sh set -e set -u export IFS= optl="Optl" LOCSTORCLI="/opt/lsi/storcli/storcli" ($LOCSTORCLI /c0 /vall show | grep RAID | cut -d " "... (5 Replies)
Discussion started by: Subsonic66
5 Replies
All times are GMT -4. The time now is 01:44 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy