How to delete a specific line from file using sed [closed] - sed

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed last year.
Improve this question
When I try to delete a specific line number from my file, all lines that have the same pattern are deleted. This is not what I want, I want to only delete the line number itself, not similar patterns.
Here is what I am trying to do:
x = 5
Command I run now:
sed -i "${x}d" home/file.txt

You had spaces around your variable assignment
x = 5
Which would be wrong.
Try the below fix
x=5
sed -i ${x}d home/file.txt

Here is an example to delete line number 33 of your file:
sed -i '33d' home/file.txt
If you need the number to be a variable:
local line_number=5
sed -i "${line_number}d" home/file.txt
The problem with what you are doing is the spaces, x = 5, will not work while x=5 will.

Here is what I am trying to do:
x = 5
That gives me:
bash: x: command not found...
, which is what I would expect. If you did not get a similar error message then you must be in the unfortunate situation of having a program named x in your path, perhaps because of doing something unwise, such as putting . in your PATH. Or perhaps you got such a message but did not see it because you have redirected your stderr.*
In any event, the quoted line does not assign a value to any shell variable. Shell variable assignments must not have whitespace around the = operator, so if you want to assign 5 to variable x, that must be
x=5
.
It is not an error to perform parameter expansion on a name that has not been assigned any value. The result is nothing. Thus, if x has not successfully been assigned any value then "${x}d" will expand to "d". As a complete sed script, that will delete every line.
*Or if you did get such an message then why in the world didn't you at least say so?

Related

Variable not being recognized after "read"

-- Edit : Resolved. See answer.
Background:
I'm writing a shell that will perform some extra actions required on our system when someone resizes a database.
The shell is written in ksh (requirement), the OS is Solaris 5.10 .
The problem is with one of the checks, which verifies there's enough free space on the underlying OS.
Problem:
The check reads the df -k line for root, which is what I check in this step, and prints it to a file. I then "read" the contents into variables which I use in calculations.
Unfortunately, when I try to run an arithmetic operation on one of the variables, I get an error indicating it is null. And a debug output line I've placed after that line verifies that it is null... It lost it's value...
I've tried every method of doing this I could find online, they work when I run it manually, but not inside the shell file.
(* The file does have #!/usr/bin/ksh)
Code:
df -k | grep "rpool/ROOT" > dftest.out
RPOOL_NAME=""; declare -i TOTAL_SIZE=0; USED_SPACE=0; AVAILABLE_SPACE=0; AVAILABLE_PERCENT=0; RSIGN=""
read RPOOL_NAME TOTAL_SIZE USED_SPACE AVAILABLE_SPACE AVAILABLE_PERCENT RSIGN < dftest.out
\rm dftest.out
echo $RPOOL_NAME $TOTAL_SIZE $USED_SPACE $AVAILABLE_SPACE $AVAILABLE_PERCENT $RSIGN
((TOTAL_SIZE=$TOTAL_SIZE/1024))
This is the result:
DBResize.sh[11]: TOTAL_SIZE=/1024: syntax error
I'm pulling hairs at this point, any help would be appreciated.
The code you posted cannot produce the output you posted. Most obviously, the error is signalled at line 11 but you posted fewer than 11 lines of code. The previous lines may matter. Always post complete code when you ask for help.
More concretely, the declare command doesn't exist in ksh, it's a bash thing. You can achieve the same result with typeset (declare is a bash equivalent to typeset, but not all options are the same). Either you're executing this script with bash, or there's another error message about declare, or you've defined some additional commands including declare which may change the behavior of this code.
None of this should have an impact on the particular problem that you're posting about, however. The variables created by read remain assigned until the end of the subshell, i.e. until the code hits a ), the end of a pipe (left-hand side of the pipe only in ksh), etc.
About the use of declare or typeset, note that you're only declaring TOTAL_SIZE as an integer. For the other variables, you're just assigning a value which happens to consist exclusively of digits. It doesn't matter for the code you posted, but it's probably not what you meant.
One thing that may be happening is that grep matches nothing, and therefore read reads an empty line. You should check for errors. Use set -e in scripts to exit at the first error. (There are cases where set -e doesn't catch errors, but it's a good start.)
Another thing that may be happening is that df is splitting its output onto multiple lines because the first column containing the filesystem name is too large. To prevent this splitting, pass the option -P.
Using a temporary file is fragile: the code may be executed in a read-only directory, another process may want to access the same file at the same time... Here a temporary file is useless. Just pipe directly into read. In ksh (unlike most other sh variants including bash), the right-hand side of a pipe runs in the main shell, so assignments to variables in the right-hand side of a pipe remain available in the following commands.
It doesn't matter in this particular script, but you can use a variable without $ in an arithmetic expression. Using $ substitutes a string which can have confusing results, e.g. a='1+2'; $((a*3)) expands to 7. Not using $ uses the numerical value (in ksh, a='1+2'; $((a*3)) expands to 9; in some sh implementations you get an error because a's value is not numeric).
#!/usr/bin/ksh
set -e
typeset -i TOTAL_SIZE=0 USED_SPACE=0 AVAILABLE_SPACE=0 AVAILABLE_PERCENT=0
df -Pk | grep "rpool/ROOT" | read RPOOL_NAME TOTAL_SIZE USED_SPACE AVAILABLE_SPACE AVAILABLE_PERCENT RSIGN
echo $RPOOL_NAME $TOTAL_SIZE $USED_SPACE $AVAILABLE_SPACE $AVAILABLE_PERCENT $RSIGN
((TOTAL_SIZE=TOTAL_SIZE/1024))
Strange...when I get rid of your "declare" line, your original code seems to work perfectly well (at least with ksh on Linux)
The code :
#!/bin/ksh
df -k | grep "/home" > dftest.out
read RPOOL_NAME TOTAL_SIZE USED_SPACE AVAILABLE_SPACE AVAILABLE_PERCENT RSIGN < dftest.out
\rm dftest.out
echo $RPOOL_NAME $TOTAL_SIZE $USED_SPACE $AVAILABLE_SPACE $AVAILABLE_PERCENT $RSIGN
((TOTAL_SIZE=$TOTAL_SIZE/1024))
print $TOTAL_SIZE
The result :
32962416 5732492 25552588 19% /home
5598
Which are the value a simple df -k is returning. The variables seem to last.
For those interested, I have figured out that it is not possible to use "read" the way I was using it.
The variable values assigned by "read" simply "do not last".
To remedy this, I have applied the less than ideal solution of using the standard "while read" format, and inside the loop, echo selected variables into a variable file.
Once said file was created, I just "loaded" it.
(pseudo code:)
LOOP START
echo "VAR_A="$VAR_A"; VAR_B="$VAR_B";" > somefile.out
LOOP END
. somefile.out

Windows Powershell- Adding to a variable in a command call

I am new to Powershell and Stack Overflow, so sorry if this an obvious question. I have searched and searched and have not found any similar questions or answers. I am trying to call a command on Powershell and changing the file name by one. Here is the line giving me trouble:
fconv -rmsd ${line}_O[int]($modeO+1).mol2 --s=${line}_A$modeA.mol2
This program takes a line of code in the format
fconv -rmsd FILE_NAME.mol2 --s=FILE_NAME.mol2
and gives a result. My problem is adding 1 to $modeO. $modeO is a number that is pulled from a file and converted to an int by using
$modeO = file.txt | Select -Index 0
[int]$modeO = [convert]::ToInt32($ModeO.Trim(), 10)
Now, whenever I try this command it says "could not open 5157_O6+1.mol2" when modeO is 6. I want it to use the file O7, but the +1 is not adding properly. I have tried separating it with parentheses, curly braces, and putting [int] in front of ($modeO+1). Is there a way to add to a variable like this while using it? Any help is appreciated, thank you!

"isn't numeric" warning from perl [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Perl keeps telling me that '11' from my input file is not numeric. Last time I checked 11 was a number, so I'm a bit confused what the problem is here and hence where to start looking.
Interestingly, it's line 1 of my input file causing the problem, the numbers on lines 2 onwards are interpreted correctly.
Argument "11" isn't numeric in numeric eq (==) at ./extract.pl line 200, <INPUTFILE> line 1.
Line 200 is
if ($_data[0] == $_ch && !exists $_ch_sn{$_data[1]}) { $_ch_sn{$_data[1]} = undef; }
Any help much appreciated!
The error was caused by a BOM in my inputfile. Using setlocal nobomb in Vi solved the problem!
It could be that the source doesn't actually contain 11 (e.g. maybe it contains a character that resembles a one but isn't), but I suspect the warning message is incorrectly identifying the expression that's non-numeric[1].
That fact that nothing remotely resembling 11 appears on that line appears to support that[2].
So just pretend you obtained the following warning:
Argument isn't numeric in numeric eq (==) at ./extract.pl line 200, <INPUTFILE> line 1.
This means that $_data[0] or $_ch is a non-numeric string[2].
If both should be numbers, determine which one isn't, possibly using the following code:
use Data::Dumper; { local $Data::Dumper::Useqq = 1; warn(Dumper($_data[0], $_ch)); }
Once you determine which variable is wrong, debug to find out why it is that way.
If they could be legitimately be non-numeric, add appropriate checks to handle that situation.
For performance reasons, that part of the error message is deduced from the compiled code, a complicated and unreliable process.
I'm assuming the if has no elsif clause. Warnings from an elsif condition can appear to be from an if statement.

How to grep the data only for the first two combination and perform > operation [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I have a file as shown below.
2.6G kishan /home/Srikishan
10G kishan /home/data/aa
150G kishan /home/Junk
300G kishan /home/junk2
I want a command which displays only the folders which are consuming more than 50G memory. Can someone help me how I can code it using shell or Perl or TCL.
As a Perl one-liner
perl -ne'/([\d.]+)G/ and $1 > 50 and print' myfile
output
150G kishan /home/Junk
300G kishan /home/junk2
This will also ignore lines that don't contain a field like 999G
And here's the Tcl contender. It looks at every line in the file whose name is in the filename variable and prints those lines that begin with a floating-point number larger than 50.
package require fileutil
fileutil::foreachLine line $filename {if {[scan $line %f] > 50} {puts $line}}
Using awk you can do:
awk -F 'G' '$1>50' file
150G kishan /home/Junk
300G kishan /home/junk2

How to change the name of the output in matlab function to other than ans [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 am new to Matlab and the answer may be very simple. I have script that runs a function to return an answer matrix, ans. How can I get Matlab to return a matrix named J instead?
Here is how I call the function: (myfunction(a,b));
I get the following error if i try to call 'myfunction = J'.
Error using myfunction (line 10)
Not enough input arguments.
Error in myfunction (line 25)
J = myfunction
If remove the line myfunction = J. I no longer get an error in line 10 any more.
Thanks
The problem was I was trying to name the output in the function where as I should have defined this in my script.
SO instead of calling the function as:
(myfunction(a,b));
Instead it should be: J=(myfunction(a,b));
It depends on how you call your script.
If you do:
> myScript;
in the command window, the result will be stored in the variable ans.
If you do:
> J = myScript;
The result will be stored in J.
Whatever function you have it should have at least one output in your case (because you say that you expect something). So for instance if your function returns one variable write it as
[T]=myfunction(a,b);
so in this case T is the name of your output instead of "ans". You need to write your function in a separate .m file and save it under the same name as is the name of function, so in this case you need to save it as myfunction.m file. It has to be in the same folder as your main code is.
See the link below
http://www.mathworks.com/help/matlab/ref/function.html