Sponsored Content
Top Forums Shell Programming and Scripting How to perform multiple operations on a number before storing to a variable? Post 302958604 by Don Cragun on Friday 23rd of October 2015 04:54:10 PM
Old 10-23-2015
Note that you can assign a new value to a variable that has already been set without creating another variable name. Instead of
Code:
linenumber=$(grep -n "string" $FILENAME | cut -d : -fi)
newlinenumber=$((linenumber - 4))

(and assuming that the cut -fi was a typo and you meant [ICODE]cut -f1[/ICODE)], you could use:
Code:
linenumber=$(grep -n "string" $FILENAME | cut -d : -f1)
linenumber=$((linenumber - 4))

and, you can get rid of the cut completely with:
Code:
linenumber=$(grep -n "string" $FILENAME))
linenumber=${linenumber%%:*}
linenumber=$((linenumber - 4))

which also keeps you from getting a syntax error if more than one line in your file contains "string" (in which case this will set linenumber to the number of the 1st line in the file containing "string" minus four). And, this could also be written as:
Code:
linenumber=$(grep -n "string" $FILENAME))
linenumber=$((${linenumber%%:*} - 4))

or:
Code:
linenumber=$(awk '/string/ {print NR - 4; exit}' $FILENAME )

or, if there is a chance that "string" won't appear in the file at all:
Code:
linenumber=$(awk '/string/ {print NR - 4; f=1; exit} END {if(!f) print -1}' $FILENAME )

(where the print -4 in the END section matches what the above bash code does if there are no matches).
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Storing Multiple Values in a Variable

Hi, My code is as below: integer i=7 while ((i <= 5 )); do # test_out is a variable which contains data separated by "^". a= `echo $test_out | cut -d"^" -f$i` echo "$a" (( i = i + 1)); done From the above code, i kept $i after f so that i can capture all the data which is... (1 Reply)
Discussion started by: sandeep_1105
1 Replies

2. Shell Programming and Scripting

Can sed perform editing operations ONLY in the matched region?

Hi: Let's suppose I want to replace all the | by > ONLY when | is between . Usually (and it works) I would do something like sed -e 's/\(\*\)|\(*\]\)/\1>\2/g' where I have to "save" some portions of the matched region and use them with the \n metacharacter. I was wondering if I could... (2 Replies)
Discussion started by: islegmar
2 Replies

3. Shell Programming and Scripting

Perform Operations on One File Conditional on Data in Another File

Hello all, I am looking for a solution to the following problem. Perl or python solutions also welcome. Given this input: And this input: I want to get this output. The rule being that if the number in the first file is < 0.9, then the corresponding two columns on... (2 Replies)
Discussion started by: hydrabane
2 Replies

4. Shell Programming and Scripting

perform 3 awk commands to multiple files in multiple directories

Hi, I have a directory /home/datasets/ which contains a bunch (720) of subdirectories called hour_1/ hour_2/ etc..etc.. in each of these there is a single text file called (hour_1.txt in hour_1/ , hour_2.txt for hour_2/ etc..etc..) and i would like to do some text processing in them. Each of... (20 Replies)
Discussion started by: amarn
20 Replies

5. Shell Programming and Scripting

Perform operations as a non root user

Hi All, I need a help in the below scenario. I want to perform few operations as a non root user but those operations can be performed only by the root user. For example I need to modify /etc/hosts file as a non root user. This is just one scenario. Could you please provide... (3 Replies)
Discussion started by: kalpeer
3 Replies

6. Shell Programming and Scripting

storing multiple values in a array variable

Am using a find command in my script .The output may be one or more. I need to store those values in a array and need to access those. Am unable to find the solution . Any help on this will be helpful. if < code> else a=<find command output which gives the file name either 1 or more> if 1... (1 Reply)
Discussion started by: rogerben
1 Replies

7. Shell Programming and Scripting

Storing multiple file paths in a variable

I am working on a script for Mac OS X that, among many other things, gets a list of all the installed Applications. I am pulling the list from the system_profiler command and formatting it using grep and awk. The problem is that I want to be able to use each result individually later in the script.... (3 Replies)
Discussion started by: cranfordio
3 Replies

8. Shell Programming and Scripting

Getting lines between patterns and perform operations

Hi, I'm currently dev'ing using awk and I'm currently stuck. Here's the file, with comments on "<--- ": Record <--- First Pattern Amount 1 <--- Amount on first transaction TotalSales 0 <--- Total Sales Prior from previous transactions Time 1:00:00 <--- Time... (9 Replies)
Discussion started by: Jin_
9 Replies

9. UNIX for Advanced & Expert Users

Passing variable as input & storing output in other variable

I have a below syntax its working fine... var12=$(ps -ef | grep apache | awk '{print $2,$4}') Im getting expected output as below: printf "%b\n" "${VAR12}" dell 123 dell 456 dell 457 Now I wrote a while loop.. the output of VAR12 should be passed as input parameters to while loop and results... (5 Replies)
Discussion started by: sam@sam
5 Replies

10. Shell Programming and Scripting

Storing multiple sql queries output into variable by running sql command only once

Hi All, I want to run multiple sql queries and store the data in variable but i want to use sql command only once. Is there a way without running sql command twice and storing.Please advise. Eg : Select 'Query 1 output' from dual; Select 'Query 2 output' from dual; I want to... (3 Replies)
Discussion started by: Rokkesh
3 Replies
STAB(5) 						      BSD File Formats Manual							   STAB(5)

NAME
stab -- symbol table types SYNOPSIS
#include <stab.h> DESCRIPTION
The file <stab.h> defines some of the symbol table n_type field values for a.out files. These are the types for permanent symbols (i.e., not local labels, etc.) used by the old debugger sdb and the Berkeley Pascal compiler pc(1). Symbol table entries can be produced by the .stabs assembler directive. This allows one to specify a double-quote delimited name, a symbol type, one char and one short of information about the symbol, and an unsigned long (usually an address). To avoid having to produce an explicit label for the address field, the .stabd direc- tive can be used to implicitly address the current location. If no name is needed, symbol table entries can be generated using the .stabn directive. The loader promises to preserve the order of symbol table entries produced by .stab directives. As described in a.out(5), an element of the symbol table consists of the following structure: /* * Format of a symbol table entry. */ struct nlist { union { const char *n_name; /* for use when in-core */ long n_strx; /* index into file string table */ } n_un; unsigned char n_type; /* type flag */ char n_other; /* unused */ short n_desc; /* see struct desc, below */ unsigned n_value; /* address or offset or line */ }; The low bits of the n_type field are used to place a symbol into at most one segment, according to the following masks, defined in <a.out.h>. A symbol can be in none of these segments by having none of these segment bits set. /* * Simple values for n_type. */ #define N_UNDF 0x0 /* undefined */ #define N_ABS 0x2 /* absolute */ #define N_TEXT 0x4 /* text */ #define N_DATA 0x6 /* data */ #define N_BSS 0x8 /* bss */ #define N_EXT 01 /* external bit, or'ed in */ The n_value field of a symbol is relocated by the linker, ld(1) as an address within the appropriate segment. N_value fields of symbols not in any segment are unchanged by the linker. In addition, the linker will discard certain symbols, according to rules of its own, unless the n_type field has one of the following bits set: /* * Other permanent symbol table entries have some of the N_STAB bits set. * These are given in <stab.h> */ #define N_STAB 0xe0 /* if any of these bits set, don't discard */ This allows up to 112 (7 * 16) symbol types, split between the various segments. Some of these have already been claimed. The old symbolic debugger, sdb, uses the following n_type values: #define N_GSYM 0x20 /* global symbol: name,,0,type,0 */ #define N_FNAME 0x22 /* procedure name (f77 kludge): name,,0 */ #define N_FUN 0x24 /* procedure: name,,0,linenumber,address */ #define N_STSYM 0x26 /* static symbol: name,,0,type,address */ #define N_LCSYM 0x28 /* .lcomm symbol: name,,0,type,address */ #define N_RSYM 0x40 /* register sym: name,,0,type,register */ #define N_SLINE 0x44 /* src line: 0,,0,linenumber,address */ #define N_SSYM 0x60 /* structure elt: name,,0,type,struct_offset */ #define N_SO 0x64 /* source file name: name,,0,0,address */ #define N_LSYM 0x80 /* local sym: name,,0,type,offset */ #define N_SOL 0x84 /* #included file name: name,,0,0,address */ #define N_PSYM 0xa0 /* parameter: name,,0,type,offset */ #define N_ENTRY 0xa4 /* alternate entry: name,linenumber,address */ #define N_LBRAC 0xc0 /* left bracket: 0,,0,nesting level,address */ #define N_RBRAC 0xe0 /* right bracket: 0,,0,nesting level,address */ #define N_BCOMM 0xe2 /* begin common: name,, */ #define N_ECOMM 0xe4 /* end common: name,, */ #define N_ECOML 0xe8 /* end common (local name): ,,address */ #define N_LENG 0xfe /* second stab entry with length information */ where the comments give sdb conventional use for .stab s and the n_name, n_other, n_desc, and n_value fields of the given n_type. Sdb uses the n_desc field to hold a type specifier in the form used by the Portable C Compiler, cc(1); see the header file pcc.h for details on the format of these type values. The Berkeley Pascal compiler, pc(1), uses the following n_type value: #define N_PC 0x30 /* global pascal symbol: name,,0,subtype,line */ and uses the following subtypes to do type checking across separately compiled files: 1 source file name 2 included file name 3 global label 4 global constant 5 global type 6 global variable 7 global function 8 global procedure 9 external function 10 external procedure 11 library variable 12 library routine SEE ALSO
as(1), ld(1), a.out(5) HISTORY
The stab file appeared in 4.0BSD. BUGS
More basic types are needed. BSD
June 10, 2010 BSD
All times are GMT -4. The time now is 06:52 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy