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'
Related
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 8 years ago.
Improve this question
So when I open a log-file, it’s all one long line. Notepad word wraps it, but it really is one line. When I open a Linux terminal, and I type "cat log-file" it formats the file and does line breaking. Is there a way to take this log-file and output it into a new file how cat displays it?
Sorry if I'm being vague, I'm new to bash, and I'm still trying to learn things.
Could you use the "sed" command to add a \n somewhere in there to break the line?
I am trying to display this file in an html file. so I wanted to replace any \n with an tag. so I'm doing sed's/\n/<br>/' <log-file >html-file but its not working. how would i go about that?
To replace all \n instances with <br>:
awk -v ORS='<br>' 1 <log-file >html-file
awk reads the input line by line (thanks to the default input record separator, RS, being \n.
ORS='<br>' sets the output record separator, ORS, to <br>
1 is merely a shorthand for {print}, which simply prints each input line terminated with ORS.
As for why your command doesn't work:
sed's/\n/<br>/' is missing a space between sed and its program, 's/...', which results in a single string that is not a valid command name.
Even if you correct that, the command will still not work as intended, because sed reads line by line, and each line is read without the terminating \n; therefore, as written, you cannot replace \n in the input, and a \n will again be appended on output - effectively, your command would be a no-op.
Update: # gniourf_gniourf points out that there is a straightforward sed solution, too, if it's OK that instead of replacing the \n instances, <br> is inserted before them - which should be fine, since the output is HTML:
sed 's/$/<br>/' <log-file >html-file
$ matches the end of each input line (without capturing anything) and "replaces" it with <br>, i.e., effectively appends <br> to the line before outputting it \n-terminated.
Should there still be a need to get rid of the \n instances, you could simply pipe to tr:
sed 's/$/<br>/' <log-file | tr -d $'\n' >html-file
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
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
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.
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