new line issue with netcat [closed] - linux

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I am using below command to send some strings to udp listening server.
echo "A 192.168.192.168" | nc -u 192.168.2.1 1234
but the server is getting trailing '\n' in echoed string.
I have tried below command too, but failed
echo "A 192.168.192.168" | nc -uC 192.168.2.1 1234
How can I remove that trailing new line character ??
Do I have any special option in nc ??

echo usually provides -n flag. This is not standard.
string A string to be written to standard output. If the first operand is -n, or if any of the operands contain a backslash ( '\' ) char‐
acter, the results are implementation-defined.
On XSI-conformant systems, if the first operand is -n, it shall be treated as a string, not an option.
I would suggest using printf
printf "A 192.168.192.168" | nc -u 192.168.2.1 1234
printf doesnt append a new line unless it is told to, and it's standard behavior.

Try using
echo -n
so
echo -n "A 192.168.192.168" | nc -u 192.168.2.1 1234
echo man page says: -n do not output the trailing newline

Related

Shell command to print the statements with N number of words present in other file [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 months ago.
Improve this question
Suppose I have a file with 3 lines:
output.txt:
Maruti
Zen
Suzuki
I used the command wc -l output.txt to get no. of lines
i got output as 3
Based on the above output I have to execute a command
echo CREATE FROM (sed -n 1p OUTPUT.txt)
echo CREATE FROM (sed -n 2p OUTPUT.txt)
echo CREATE FROM (sed -n 3p OUTPUT.txt)
:
:
echo CREATE FROM (sed -n np OUTPUT.txt)
Can you please suggest a command to replace 1 2 3 .....n in the above command based on the output i get (i.e., no. of lines in my file)
I just gave a sample explanation of my use case. Please suggest a command to execute n no. of times.
You just need one command.
sed 's/^/CREATE FROM /' output.txt
See also Counting lines or enumerating line numbers so I can loop over them - why is this an anti-pattern?

Cut a string after certain a specific character, but just one field [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
This is from a vhost file. This is the output I get
ServerName uat3-dam-something1.prg-dc.brb.com
Hello,
I'm wondering how to cut from this output so only this part remains
something1.prg-dc.brb.com
Keep in mind that "something" could be "something4141411" or "something23". So length operations won't work. Tried with cut command and AWK, but didn't work. I would be happy receive a tips from the bash experts :)
Like this :
grep -o 'something.*' file
or more specific:
grep -oE 'something[0-9]+\..*' file
 Output:
something1.prg-dc.brb.com
Could you please try following, written and tested with provided samples only.
awk -F'uat3-dam-' '{print $NF}' Input_file
Description: Making uat3-dam- as field separator and printing last field of it.
2nd solution:
awk 'match($0,/something.*/){print substr($0,RSTART,RLENGTH)}' Input_file
Using:
echo "ServerName uat3-dam-something1.prg-dc.brb.com" |cut -d\- -f3-4
Will return:
something1.prg-dc.brb.com
And if you change the string (as you mention):
echo "ServerName uat3-dam-something111111.prg-dc.brb.com" |cut -d\- -f3-4
It will keep returning:
something111111.prg-dc.brb.com
$ echo 'ServerName uat3-dam-something1.prg-dc.brb.com' | awk -F- '{sub(".*" $2 FS,"")}1'
something1.prg-dc.brb.com
This will work:
echo "ServerName uat3-dam-something1.prg-dc.brb.com" | sed -E 's/.*(something.*)/\1/'
Or, if the string is in a file named file
sed -E 's/.*(something.*)/\1/' file
Explanation:
-E is for extended regex
.*(something.*) means "any char 0 or more times followed by something and any other char 0 or more times".
\1 is used to print only the matching part inside the brackets.
You could also use :
echo ${test#*dam-}
Example :
test="ServerName uat3-dam-something1.prg-dc.brb.com"
echo ${test#*dam-}
which gives:
something1.prg-dc.brb.com
Note that the opposite version would be echo ${test%something*}

How can i took specific word from line basic in linux [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 9 years ago.
Improve this question
Suppose i have one line Script :
Script Name is script1.sh has below line on it -
# sh script.sh #
So how can i take only script.sh name from script1.sh.
What I have done is below but that is not fully fruitful to me get the exact output that I want.
while read line
do
called_script= awk -F ':' '{print $1 }' final_calling_script_name
qwe= grep '*.sh' $called_script
echo $called_script " : $qwe"
done<'file_that_contains_data_of_script1_line_by_line'
Can anybody help me?
If what you want here is basically "the second word" you can use "cut"
echo "sh script.sh" | cut -d ' ' -f 2
The -d ' ' tells cut that the "delimiting character" is a space, the -f 2 tells cut that you want column number 2.
echo "sh script.sh" | { read a b; echo "$b"; }
EDIT:
After you've clarified your requirements in the notes below, I would propose this command:
echo "script1.ksh script2.pig script3.sh" | grep -oe '\w*\.sh'

How to hide the command when using command repetition with the exclamation mark? [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
When I use ! to execute a previous command it automatically prints the command as well. Is there a way to hide this?
Example:
This is what happens:
ralgrad:~$ echo test
test
ralgrad:~$ !!
echo test
test
This is what I would want:
ralgrad:~$ echo test
test
ralgrad:~$ !!
test
I have looked at the bash source and there is no way to disable this automatic printing of the expanded command. You would have to compile your own version of bash!
If it is particularly important to you for whatever reason, look in bashhist.c in the pre_process_line function and comment out/remove the following line:
printf (stderr, "%s\n", history_value);
You cannot do that with !!, because it repeats the last command not the last output. As far as I know, there is no single command that allows to achieve what you are asking. However, you can try a simple hack:
result=`echo test`
echo "$result"
Well, you can simulate what you want with the following batch file:
if [ -z $1 ]; then
exe=`fc -lrn | awk 'NR==2 {print;}'`
else
exe=`fc -lrn | cut -f2- | awk 'NR>=2 {print;}' | grep "^ $*" | head -n1 `
fi
eval $exe
Let h be the name of the file (or, if you want it, even !). Then you can do:
# echo Foo
Foo
# echo Bar
Bar
# h
Bar
# h echo F
Foo

Suppress Non-Matching Lines in Grep [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 11 years ago.
Improve this question
Running service --status-all | grep "firestarter" in Ubuntu shows the entire output of service --status-all with the text "firestarter" highlighted in red. How do you get grep to only show the line that contains the matched text, and hide everything else?
Maybe service --status-all writes to stderr, not stdout? Then you can use
service --status-all 2>&1 | grep firestarter
You must have some weird env variables set. Try this:
service --status-all | `which grep` firestarter
Or:
service --status-all | /bin/grep firestarter
And show the output of env and alias if possible so we can see whats wrong with your grep command.
For me, I have:
[ 13:55 jon#host ~ ]$ echo $GREP_OPTIONS
--color=always
You probably have something set there, and/or in GREP_COLOR that is causing this.
if you don't want to use an alias, but the original command, you could try "\cmd". e.g.
service --status-all | \grep "firestarter"

Resources