Suppose you have the following script:
# My comment line 1
# My comment line 2
# My comment line 3
echo "My script"
cd $MY_PATH
./anotherScript.ksh
Is there any command to show:
# My comment line 1
# My comment line 2
# My comment line 3
Alternatively, I could write a script to detect the first block of comments.
Thanks
Try this:
grep '^\#.*$' myscript.sh
You could cat script.sh | grep ^\# to only show those lines.
This will output your file to the grep command which will print each line with a "#"
echo "My script" | grep "#"
EDIT I'M DUMB
all you need to do is grep '#' file if I'm understanding correctly this time.
Related
This question already has answers here:
Bash tool to get nth line from a file
(22 answers)
Closed 4 years ago.
I am currently using the following to save terminal outputs to file:
$command -someoptions >> output.txt
However, I am only interested in one line from the terminal output.
Is there a way to do this by changing the above expression. Or will I have to delete lines after the 'output.txt' file is formed?
For example: If my output is:
line 1
line 2
line 3
line 4
line 5
and all I want to save is:
line 4
where line 4 contains unknown information.
I am asking as I will later wish to script this command.
Many thanks,
Solution Found:
I ended up using:
$command -someoptions | sed -n '4p' >> output.txt
This is a classic simple grep issue.
$command -someoptions | grep 'line 4' >> output.txt
You could refine that with more pattern complexity, and might need it depending on how precisely you need to match the data.
Try with this command:
$command -someoptions | grep " filter " >> output.txt
filter must be replaced by an element that distinguishes your line 4 from the other lines.
This question already has answers here:
Displaying only single most recent line of a command's output
(2 answers)
Closed 5 years ago.
I been trying to print a variable in the same time for a scrip that pretends automatize a process the content is the output of this
sed "s/Read/\n/g" /tmp/Air/test.txt | tail -1 test.txt | grep ARP
so i put this in a while loop
do
out= sed "s/Read/\n/g" /tmp/Air/test.txt | tail -1 test.txt | grep ARP
echo -n "$out"
sleep 1
done
i read other questions here and i try with different option like echo -ne, echo -ne "$out" \r, printf "\r" or printf "%s" and no luck with no one, all the other example don't have a variable to print just counter o system variables
Update
it seems to appear that the echo -n repeat $out in the same line, if out="this is a test" the output of echo -n is "this is a test this is a test this is a test this is a test ...." maybe im missing some option ?
Update 2
sorry for the miss understood perhaps i was not very clear but what i want is overwrite the same line with the value of $out, the source of $out is the output of the aireplay-ng command that executes along with the script and i get the output with
the ouput is something like this
102415 packets (got 5 ARP requests and 15438 ACKs), sent 37085 packets...(499 pps)
but the number of ARP request is changing constantly
this code for example use echo -ne and overwrite in the same line
#!/bin/bash
for pc in $(seq 1 100); do
echo -ne "$pc%\033[0K\r"
sleep 1
done
the output of this is like a percent indicator that shows "10%" and going instead of "1% 2% 3% 4% 5% .." in the same line and i already try like this but with no luck
if you are trying to execute the sed Please use
`sed "s/Read/\n/g" /tmp/Air/test.txt | tail -1 test.txt | grep ARP`
First of all you are assigning a value of bash command wrongly to variable.
out=$(sed "s/Read/\n/g" /tmp/Air/test.txt | tail -1 test.txt | grep ARP)
Then you can print all your output in one line as you wrote:-
echo -n $out
The recent addendum to your question reads like you're miscommunicating your intent: this is a test this is a test this is a test is what a plain reading of your question indicates you to be asking for (printing this is a test over and over in a loop without newlines, after all, can be expected to do nothing else); why you'd describe this in a context that makes it sound like a bug is thus surprising.
If you want to send the cursor back to the beginning of your current line and overwrite that line, that might be something like the following:
#!/bin/bash
# ^^^^ not /bin/sh; this enables bash extensions
# ask the shell to keep $COLUMNS up-to-date
shopt -s checkwinsize
# defaults to 80-character terminal width, but uses $COLUMNS if available
printf "%-${COLUMNS:-80}s\r" "$out"`
...which prints your string, pads out to 80 characters with spaces, and then returns the cursor to the beginning of the line, such that the next thing you write will overwrite that string.
Of course, if you print that line and then return to a shell prompt, the prompt will start at the beginning of the same line and overwrite the text, so be sure to follow up with an echo.
I am using this command in a shell script
lnum=5
str="Hello foo"
filename="/path/fiename"
sed -i "$lnum i $str" $filename
Getting the following error
sed: -e expression #1, char 3: : doesn't want any addresses
I had used this command before for other script it worked fine, the only change i made this time is file-name has a path to the file, but I tried it with just giving file-name and not the path by getting into the path and executing the script but still it doesn't work
I am unable to resolve it can anybody help
If you are using OSX , BSD (and AIX) versions of sed, the backup extension for the -i in place editing flag is not optional.
GNU sed differs on this I believe, so the script may work on Linux.
This is a bit of a pain for portability - but it gets worse with "in-place" editing when BSD derived sed is used. This version of sed is arguably more "standard" in some ways (as in: "lowest common denominator across POSIX systems") but this behaviour seems like a bug:
sed: 1: "5 i hello foo": command i expects \ followed by text
Here is how I made your script work on several BSD flavors:
lnum="5"
str="Hello foo"
filename="sed-mess.txt"
sed -i "" "$lnum i\^M
$str" $filename
I had to enter a literal line end character with Ctrl-v [Return] to get the i command to work since \ is required and has to have nothing following it. Not sure how GNU sed would handle this.
Can you use perl ? ;-)
The old farts use ed for this type of problem:
cat /tmp/sample
#!/bin/ksh
lnum=5
str="Hello bar"
filename="/tmp/foo"
ed - $filename <<EOF
$lnum
i
$str
.
w
q
EOF
cat /tmp/foo
line
line
line
line
line
line
line
line
line
wc /tmp/foo
9 9 45 /tmp/foo
/tmp/sample
line
wc /tmp/foo
10 11 55 /tmp/foo
cat /tmp/foo
line
line
line
line
Hello bar
line
line
line
line
line
i is "insert" before while a is "append" after. I don't know what the sed i command does exactly but I would expect it would be the same as i
Hope this helps
I have a text file, say, text.
What I want to do is to put the text file into pipe (as in cat text |), but with added line at top and bottom - but without creating a new file (because the text file is kind of big) or modifying it.
Is that possible? I was thinking echo $(echo "line"; cat text; echo "line") but I don't like that.
From man bash:
(list) list is executed in a subshell environment (see COMMAND EXECUTION ENVIRONMENT below). Variable assignments and builtin commands that affect the shell's environment do not remain in effect after the command completes.
The return status is the exit status of list.
So this should work:
(echo first line; cat file; echo last line) | some_command
As suggested by chepner, when you don't need a subshell you can use {} instead of (). In this case, the ; is mandatory including for the last command in the group.
In the comments Pyrolistical remembers this also works if you have an incoming pipe instead of a file:
some_program | (echo first line; cat -; echo last line) | some-other-command
As in many unix command line applications, if the filename argument for cat is - it means "read from standard input".
Copied from this question Add new lines to top and bottom of every text file:
cat text |sed -e '1 i beginning_line'| sed -e '$s#$#\nending_line#' |
it seems to work.
add lines to the pipe output,
add a line to the top: (source - #Karel Bilek's comment)
cat text.txt | sed -e '1 i my first line'
add a line to the bottom: ( source - https://askubuntu.com/a/968585 )
cat text.txt | sed -e '$a my last line'
e.g: try this with single quotes
cat text.txt | sed -e '1 i my first line' | sed -e '$a my last line'
output:
my first line
this is
my
text
file
my last line
I am using SunOS 10. I am trying to replace the : character at the end of line if the word contains : in it.
I am using the below command for it.
echo -n "test:" | sed 's/:$//g'
It's not working. What did I do wrong here?
The same command is working fine in GNU/Linux.
You don't need a line feed. You need to remove that -n
echo "test:" | sed 's/:$//g'
myshell:/home/myfolderpath # echo -n "test:"|sed 's/:$//g'
testmyshell:/home/myfolderpath#
you code works on my machine.
because there is no tailing new line.you gonna see the result right before your next shell command line. -n is not necessary.
myshell:/home/myfolderpath # echo "test:"|sed 's/:$//g'
test
myshell:/home/myfolderpath#
it should be like this without -n