Thanks,
That works well, but not easy to understand, can you clear my mind please
Thanks.
Hello fongthai,
Could you please go through following and let us know if this helps you. Also please do not run following, as following one is only for understanding purposes, I have split it.
Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
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:
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:
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:
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:
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.
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.
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.
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.
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:
Thanks Don,
You're more than expected pointing out my requirement changed, showing your script and explain the different comparing to Ravinder's one. Perfect!
The reason I changed it just to make it easier for people to help me.
I also want to send my thanks to Ravinder as your clear explanation.
Two things to note.
This process creates a new file without modifying the original. You can run this process as many times as you like. Don't forget to delete the outputfile if you re-run.
Secondly, in the 'if' statement the 'a' is added to the beginning of the strings to prevent the script dying if there is a blank line in the input file.
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)
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)
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)
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)
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)
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)
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)
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)
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)