Sponsored Content
Top Forums Shell Programming and Scripting Swapping or switching 2 lines using sed Post 302259792 by Franklin52 on Wednesday 19th of November 2008 03:03:28 AM
Old 11-19-2008
Hi,

Code:
s1=s1?s1 "\n" $0:$0

This is a conditional expression operator. The format is : condition ? expression1 : expression2.
If the condition is true, the result is expression1 else the result is expression2.

With our command we check the value of s1 (s1?). If s1 is true (s1 is not empty) we assign the contents of s1, a newline ("\n") and the current record ($0) to s1. If the condition is false (s1 is empty) we assign the current record ($0) to s1.

Regards
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Swapping lines beginning with certain words using sed/awk

I have a large file which reads like this: fixed-address 192.168.6.6 { hardware ethernet 00:22:64:5b:db:b1; host X; } fixed-address 192.168.6.7 { hardware ethernet 00:22:64:5b:db:b3; host Y; } fixed-address 192.168.6.8 { hardware ethernet 00:22:64:5b:db:b4; host A; }... (4 Replies)
Discussion started by: ksk
4 Replies

2. Shell Programming and Scripting

swapping lines that match a condition using sed, perl or the like

I'm a bit new to regex and sed/perl stuff, so I would like to ask for some advice. I have tried several variations of scripts I've found on the net, but can't seem to get them to work out just right. I have a file with the following information... # Host 1 host 45583 { filename... (4 Replies)
Discussion started by: TheBigAmbulance
4 Replies

3. Shell Programming and Scripting

awk: switching lines and concatenating lines?

Hello, I have only recently begun with awk and need to write this: I have an input consisting of a couple of letters, a space and a number followed by various other characters: fiRcQ 9( ) klsRo 9( ) pause fiRcQ 9( ) pause klsRo continue 1 aPLnJ 62( ) fiRcQ continue 5 ... and so on I... (7 Replies)
Discussion started by: Borghal
7 Replies

4. Homework & Coursework Questions

Swapping Fields with Sed

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: The assignment is to convert a text table to csv format. I've got the cleaning up done, but I need to swap two... (0 Replies)
Discussion started by: VoiceInADesert
0 Replies

5. Shell Programming and Scripting

Swapping three lines

I have some text: <date>some_date</date> <text>some_text</text> <name>some_name<name> and I want to transform it to smthng like that: some_name on some_date: some_text I've tried sed: sed 's/<text>\(.*\)<\/text> <name>\(.*\)<\/name>/\2 - \1/' but it says unterminated... (13 Replies)
Discussion started by: dsjkvf
13 Replies

6. Shell Programming and Scripting

Switching lines

Hi I'm quite new with linux. Very simple, I need to swap every 2 lines in a file. Example INPUT: a a a b b b x x x y y y s s s t t t OUTPUT: b b b a a a y y y x x x t t t (5 Replies)
Discussion started by: hernand
5 Replies

7. Shell Programming and Scripting

AWK swapping fields on different lines

Hi All, Sorry if this question has been posted elsewhere, but I'm hoping someone can help me! Bit of an AWK newbie here, but I'm learning (slowly!) I'm trying to cobble a script together that will save me time (is there any other kind?), to swap two fields (one containing whitespace), with... (5 Replies)
Discussion started by: Bravestarr
5 Replies

8. Shell Programming and Scripting

ksh sed - Extract specific lines with mulitple occurance of interesting lines

Data file example I look for primary and * to isolate the interesting slot number. slot=`sed '/^primary$/,/\*/!d' filename | tail -1 | sed s'/*//' | awk '{print $1" "$2}'` Now I want to get the Touch line for only the associate slot number, in this case, because the asterisk... (2 Replies)
Discussion started by: popeye
2 Replies

9. Shell Programming and Scripting

Swapping the 1st 4 lines only

How can you swap the first 4 line only, the rest will stay the same. thanks #!/bin/sh line=4 awk -v var="$line" 'NR==var { s=$0 getline;s=$0"\n"s getline;print;print s next }1' fileko.tx . desired output: (8 Replies)
Discussion started by: invinzin21
8 Replies

10. Shell Programming and Scripting

Swapping lines

Hi there, I have a text that I'm trying to format into something more readable. However, I'm stuck in the last step. I've searched and tried things over the internet with no avail. OS: Mac After parsing the original text that I won't put here, I managed to get something like this, but this... (8 Replies)
Discussion started by: Kibou
8 Replies
expr(1) 						      General Commands Manual							   expr(1)

NAME
expr - Evaluates arguments as expressions SYNOPSIS
expr expression STANDARDS
Interfaces documented on this reference page conform to industry standards as follows: expr: XCU5.0 Refer to the standards(5) reference page for more information about industry standards and associated tags. OPTIONS
None OPERANDS
The single expression evaluated by expr will be formed from the operands, as described in the DESCRIPTION section. Each of the expression operator symbols: ( ) | & = > >= < <= != + - * / % : and the symbols integer and string in the DESCRIPTION sec- tion must be provided as separate arguments to expr. Expression to be evaluated, formed as explained in the DESCRIPTION section. DESCRIPTION
The expr command reads an expression, evaluates it, and writes the result to standard output. Within the expression argument, you must separate each term with spaces, precede characters special to the shell with a (backslash), and quote strings containing spaces or other special characters. Note that expr returns 0 to indicate a zero value, rather than the null string. Integers can be preceded by a unary minus sign. Internally, integers are treated as 64-bit or 32-bit two's complement numbers, depending on the word size of the hardware platform. The operators and keywords are described in the following listing. Characters that need to be escaped are preceded by a (backslash). The list is in order of increasing precedence with equal precedence operators grouped within {} (braces). Returns expression1 if it is neither null nor 0 (zero); otherwise, returns expression2. Returns expression1 if neither expression1 nor expression2 is null nor 0; oth- erwise, returns 0. Returns the result of an integer comparison if both expressions are integers; otherwise, returns the result of a string comparison. Adds or subtracts integer-valued arguments. Multiplies, divides, or provides the remainder from the division of integer-val- ued arguments. Compares expression1 with expression2, which must be a basic regular expression, with syntax as described for grep, except that all patterns are anchored, so ^ (circumflex) (which anchors a pattern to the beginning of a line) is not a special character in this context. Normally, the matching operator returns the number of characters matched. Alternatively, you can use the (...) symbols in expres- sion2 to return a portion of expression1. Provides expression grouping. An argument consisting only of an (optional) unary minus followed by digits. A string argument. Refer to the STRING OPERAND section. To avoid unpredictable results when using a range expression to match a class of characters, use a character class expression rather than a standard range expression. For information about character class expressions, see the discussion of this topic included in the description of the grep command. FUNCTIONS [Tru64 UNIX] The expr command provides the following string-expression functions. Strings containing white space must be quoted. A string argument should not be one of the expression operator symbols shown in the OPERANDS section or one of the string function names below. You cannot use, for example, match "$subject" "$pattern" unless it is confirmed that subject and pattern do not match this viola- tion. The first character of a string is position 1. The information returned by all these functions is in the form of a string: [Tru64 UNIX] Compares each character in the second string against each character in the first string and returns the position in the first string of the first match found, where the first match is the match closest to the beginning of string1. [Tru64 UNIX] Returns the length of the string argument. [Tru64 UNIX] See the description of the : (colon) match operator, above. [Tru64 UNIX] Returns the substring of string that begins at character position start and is length characters long. STRING OPERAND A string argument is an argument that cannot be identified as an integer argument or as one of the expression operator symbols shown in the OPERANDS section. The use of string arguments length, substr, index or match produces unspecified results. EXIT STATUS
The expr command returns the following exit values: The expression is neither null nor 0. The expression is null or 0. The expression is invalid. An error occurred. EXAMPLES
To increment a shell variable, enter: COUNT=`expr $COUNT + 1` This adds 1 to the COUNT shell variable (see sh for details). To find the length of a shell variable, enter: RES=`expr "$VAR" : ".*"` Note that VAR is in double quotes to avoid problems where VAR is NULL or contains embedded spaces. The regular expression is also quoted to avoid expansion by the shell. This operation can also be performed as: RES=`expr length "$VAR"` To use part of a shell variable, enter: RES=`expr "$VAR" : "-*(.*)"` This removes leading - (dashes), if any, from VAR. If the ( ) characters were omitted, RES would contain the length of VAR. To find a character from one string in another string, enter: INDEX=`expr index "Hello World!" "Wo"` Note that the returned value is 5, not 7. The first match is the fifth character in string1, matching the o in string2 rather than the W, which is the seventh character in string1. Special considerations: RES=`expr "x$VAR" : "x-*(.*)"` This succeeds even if VAR has the value - (dash). RES=`expr "x$VAR" = "x=" This succeeds even if VAR has the value = (equal sign). ENVIRONMENT VARIABLES
The following environment variables affect the execution of expr: Provides a default value for the internationalization variables that are unset or null. If LANG is unset or null, the corresponding value from the default locale is used. If any of the internationalization vari- ables contain an invalid setting, the utility behaves as if none of the variables had been defined. If set to a non-empty string value, overrides the values of all the other internationalization variables. Determines the locale for the interpretation of sequences of bytes of text data as characters (for example, single-byte as opposed to multibyte characters in arguments) and the behavior of character classes in regular expressions.. Determines the locale for the format and contents of diagnostic messages written to standard error. Determines the location of message catalogues for the processing of LC_MESSAGES. SEE ALSO
Commands: grep(1), Bourne shell sh(1b), POSIX shell sh(1p), test(1) Functions: string(3), wcscat(3) Standards: standards(5) expr(1)
All times are GMT -4. The time now is 04:30 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy