Unix header and footer matching patterns condition [closed] - shell

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I need to move my text files from processing folder to backup folder by reading the files in directory, each text file contains header, footer and other records. before moving to backup I need check that header should start with 01 and footer should start 99. If the condition satisfies i should move otherwise skip the current file and continue with other files. How to write a condition to check the 1st line should strat with 01 and last line should start with 99.
Please help me..Thanks in advance.
Sreeni

Try below:
cat file | head -1 | grep "^01" #check the first line start with 01
cat file | tail -1 | grep "^99" #check the last line start with 99
If "^" doesn't work just replace it with "/>". Both mean starting with.

you can use awk to do it, first write a awk script,e.g. t.awk
NR==1{if($1~/^01/)print}
END{if($1~/^99/)print}
and then,use awk -f t.awk your_file_name
hope to help you.

Related

lsof, grepping output of [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 7 years ago.
Improve this question
To count open epubs I used this:
# - determine how many epubs are open -
NUMBER_OF_OPEN_EPUBS=0
while read -r LINE ; do
if [ "$(echo $LINE | rev | cut -c1-5 | rev)" = ".epub" ]; then
NUMBER_OF_OPEN_EPUBS="$(($NUMBER_OF_OPEN_EPUBS+1))"
fi
done < <(lsof | grep "\.epub")
# --- end determine how many epubs are open ---
and it always worked. But I wanted to extend it to fb2 files (similar to epubs) as well so I got an fb2 for testing and couldn't make it work. To illustrate the underlying problem in it's simplest form:
With 2 files, /test.epub & /test.fb2 open in fbreader in seperate windows, in bash, in lxterminal, under Ubuntu 14.04 LTS and plain Openbox:
me#nu:~$ lsof | grep "\.fb2" | tr -s " "
me#nu:~$ lsof | grep "\.epub" | tr -s " "
fbreader 28982 me 12r REG 8,5 346340 8375 /test.epub
me#nu:~$
Why doesn't lsof see the fb2? In practical terms, I suppose I could use ps, which exhibits no prejudice against fb2 files (and incidentally proves grep isn't to blame) instead, but why does lsof snub fb2 files?
==================
P.S. I edited this to put it in proper context here, even though, thanks to Mr.Hyde, it is solved already. The question as stated reflects an implied and unexamined assumption which turns out to be false. See answer.
Hyde's comment was the clue I needed. So he should get the credit. I'm not clear how that works yet, but I've lurked this site enough to know it is important to y'all.
So, right, if fbreader keeps one file open but not the other, as Hyde suggested, the question would be why. I was assuming the file type was the important thing, but once I looked at it that way the possibility was obvious and I tested it and the issue isn't type but file size. I've only found one fb2 to test my script with and it happens to be smaller than most of my epubs. I found a small epub and it behaved the same way. Presumably if the file is small enough fbreader just stores the whole thing in memeory but can't for a larger file. So, mea culpa, the problem as stated is spurious. Bottom line from a scripting pov is I need to use
ps -eo args
instead of
lsof
because ps sees the file as an argument to a command that started an open process rather than an open file and that is probably more to the point anyway. Thanks, gentles.

Extracting string in BASH using grep [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I`m a bit new to bash, so it might be simply task. What I need is to extract the TITLE from file which contains lines like this one:
Title: Blaze Of Glory Track: 3
All I need is just a regular expression using grep or sed.
Guess I would figure it out myself, but I`m a bit in hurry:<
Appreciate your help!
This is just to clean up then answer form Mark. (removes space in front and track info)
title=$(awk -F: '/^Title/{gsub(/(^Title: | Track:.*$)/,"");print}' file)
echo $title
Blaze Of Glory
Or to make sure all spaces/tabs etc are gone:
awk -F: '/^Title/{gsub(/(^Title:[[:space:]]*|[[:space:]]*Track:.*$)/,"");print}'
Like this:
title=$(awk -F: '/^Title/{print $2}' file)
echo $title
Output:
Blaze Of Glory Track

Throw away all text after a blank line in bash [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
Using bash, I want to throw away all of the text in a file after the first blank line. The blank line is used as a delimiter between records, and I only want the first record in the file. Unfortunately, the number of lines per record can change depending on what the record refers to exactly, so I can't just keep the first n lines as a global solution.
[EDIT] Here is a solution that works:
qstat -f > out.tmp
grep -A90 -B0 $1 out.tmp > out2.txt
awk '/^$/{exit}{print}' out2.txt
rm out.tmp out2.txt
where $1 points to the name of the text file to be analyzed (passed as an argument to the script that I'm writing). Thanks.
You can do this with pretty much any generic text processing tool, e.g.:
awk '$0==""{exit}{print}'
awk '/^$/{exit}{print}'
sed '/^$/q'

I want to delete a string from each line in a file [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a file whose contents are
/a/b/c
/a/b/d/xyz
/a/b/c/nmxnxlcs
...
I want to delete the string /a/b/ from the file.
I want to do it using shell script.
With sed:
sed 's#/a/b/##' file
if you want to update the file,
sed -i.bak 's#/a/b/##' file
it will create a backup file.bak and update file with the new values.
As mbratch comments in his answer, it can happen that you just want to replace lines starting with /a/b/. In that case, you can use:
sed 's#^/a/b/##' file
where ^ stands for beginning of line.
Test
$ cat a
/a/b/c
/a/b/d/xyz
/a/b/c/nmxnxlcs
hello/a/b/aa
$ sed 's#/a/b/##' a
c
d/xyz
c/nmxnxlcs
helloaa
Although it's not mentioned in the problem statement, the example suggests that you only want to delete the string if it's at the beginning of the line:
sed 's:^/a/b/::' myfile.txt
This will change:
/a/b/c/foo.txt
To:
c/foo.txt
And will change:
/a/b/c/x/a/b/foo.txt
To:
c/x/a/b/foo.txt
And not to:
c/xfoo.txt

Include a file to an executable script, grab line 1 and line 2, assign them to data_holders [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
Assume that there is a file named a.txt
which contains:
aaa
bbb
i need to create an executable script that
will grab these 2 lines from the a.txt
and then print them in the terminal.
in other words when i run..
./script
it needs to print
aaa:bbb
UPDATE
i need to assign the first line from the file a.txt
to the letter "a"
i need to assign the second line from the file a.txt
to the letter "b"
so that i can access "a" and "b" within my bash script
separately.
You can use awk to do this:
awk '{printf $1":"}' a.txt

Resources