Sponsored Content
Top Forums Shell Programming and Scripting Join 2 separate strings into one with alternate tokens. Post 302536365 by ctsgnb on Tuesday 5th of July 2011 08:12:43 AM
Old 07-05-2011
@sikku, try again separating the option of pr command, and make sure you didn't forget anything in the sed syntax.

Code:
echo "$string1 $string2"  | tr ' |' '\n' | pr -2 -t -s: | sed 's/^\([^:]*\):\([^:]*\)/\2:\1/' | xargs

---------- Post updated at 02:12 PM ---------- Previous update was at 02:10 PM ----------

then you can pass the statement step by step to see what doesn't behave as expected :

See how the final output is built step by step :

Code:
[ctsgnb@shell ~/sand]$ echo "$string1 $string2"
abc|def|ghi 123:2|124:1|125:3
[ctsgnb@shell ~/sand]$ echo "$string1 $string2"  | tr ' |' '\n'
abc
def
ghi
123:2
124:1
125:3
[ctsgnb@shell ~/sand]$ echo "$string1 $string2"  | tr ' |' '\n' | pr -2 -t -s:
abc:123:2
def:124:1
ghi:125:3
[ctsgnb@shell ~/sand]$ echo "$string1 $string2"  | tr ' |' '\n' | pr -2 -t -s: | sed 's/^\([^:]*\):\([^:]*\)/\2:\1/'
123:abc:2
124:def:1
125:ghi:3
[ctsgnb@shell ~/sand]$ echo "$string1 $string2"  | tr ' |' '\n' | pr -2 -t -s: | sed 's/^\([^:]*\):\([^:]*\)/\2:\1/' | xargs
123:abc:2 124:def:1 125:ghi:3
[ctsgnb@shell ~/sand]$

 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

tokens in unix ?

im trying to remove all occurences of " OF xyz " in a file where xyz could be any word assuming xyz is the last word on the line but I won't always be. at the moment I have sed 's/OF.*//' but I want a nicer solution which could be in pseudo code sed 's/OF.* (next token)//' Is... (6 Replies)
Discussion started by: seaten
6 Replies

2. Shell Programming and Scripting

: + : more tokens expected

Hello- Trying to add two numbers in a ksh shell scripts and i get this error every time I execute stat1_ex.ksh: + : more tokens expected stat1=`cat .stat1a.tmp | cut -f2 -d" "` stat2=`cat .stat2a.tmp | cut -f2 -d" "` j=$(($stat1 + $stat2)) # < Here a the like the errors out echo $j... (3 Replies)
Discussion started by: Nomaad
3 Replies

3. UNIX for Dummies Questions & Answers

How to join two strings together

There is a file: !EN ih n w ey I want to join the current instance with its previous instance together, such as: previous_instance-B+current_instance, there "-B+" is fixed iterm, the file after operate look like: !EN start-B+!EN ih !EN-B+ih n ih-B+n w... (1 Reply)
Discussion started by: Jenny.palmy
1 Replies

4. Programming

sql,multiple join,outer join issue

example sql: select a.a1,b.b1,c.c1,d.d1,e.e1 from a left outer join b on a.x=b.x left outer join c on b.y=c.y left outer join d on d.z=a.z inner join a.t=e.t I know how single outer or inner join works in sql. But I don't really understand when there are multiple of them. can... (0 Replies)
Discussion started by: robbiezr
0 Replies

5. Shell Programming and Scripting

Replacing tokens

Hi all, I have a variable with value DateFileFormat=NAME.CODE.CON.01.#.S001.V1.D$.hent.txt I want this variable to get replaced with : var2 is a variable with string value DateFileFormat=NAME\\.CODE\\.CON\\.01\\.var2\\.S001\\.V1\\.D+\\.hent\\.txt\\.xml$ Please Help (3 Replies)
Discussion started by: abhinav192
3 Replies

6. UNIX for Dummies Questions & Answers

Join 2 files with multiple columns: awk/grep/join?

Hello, My apologies if this has been posted elsewhere, I have had a look at several threads but I am still confused how to use these functions. I have two files, each with 5 columns: File A: (tab-delimited) PDB CHAIN Start End Fragment 1avq A 171 176 awyfan 1avq A 172 177 wyfany 1c7k A 2 7... (3 Replies)
Discussion started by: InfoSeeker
3 Replies

7. UNIX for Dummies Questions & Answers

How to use the the join command to join multiple files by a common column

Hi, I have 20 tab delimited text files that have a common column (column 1). The files are named GSM1.txt through GSM20.txt. Each file has 3 columns (2 other columns in addition to the first common column). I want to write a script to join the files by the first common column so that in the... (5 Replies)
Discussion started by: evelibertine
5 Replies

8. Programming

Reading tokens

I have a String class with a function that reads tokens using a delimiter. For example String sss = "6:8:12:16"; nfb = sss.nfields_b (':'); String tkb1 = sss.get_token_b (':'); String tkb2 = sss.get_token_b (':'); String tkb3 = sss.get_token_b (':'); String tkb4 =... (1 Reply)
Discussion started by: kristinu
1 Replies

9. Shell Programming and Scripting

Single grep to multiple strings with separate output per string

I need to grep multiple strings from a particular file. I found the use of egrep "String1|String2|String3" file.txt | wc-l Now what I'm really after is that I need to separate word count per each string found. I am trying to keep it to use the grep only 1 time. Can you guys help ? ... (9 Replies)
Discussion started by: nms
9 Replies

10. Shell Programming and Scripting

Help to join separate lines in a single one from xml file

Hi all, I need help to parse this xml file that has paragraphs broken in different lines and I would like to join in a single line. I hope you can understand my explanation. Thanks for any help/direction. The script could be in bash, awk, ruby, perl whatever please In the output I want:... (8 Replies)
Discussion started by: Ophiuchus
8 Replies
TR(1)							    BSD General Commands Manual 						     TR(1)

NAME
tr -- translate characters SYNOPSIS
tr [-Ccsu] string1 string2 tr [-Ccu] -d string1 tr [-Ccu] -s string1 tr [-Ccu] -ds string1 string2 DESCRIPTION
The tr utility copies the standard input to the standard output with substitution or deletion of selected characters. The following options are available: -C Complement the set of characters in string1, that is ``-C ab'' includes every character except for ``a'' and ``b''. -c Same as -C but complement the set of byte values in string1. -d Delete characters in string1 from the input. -s Squeeze multiple occurrences of the characters listed in the last operand (either string1 or string2) in the input into a single instance of the character. This occurs after all deletion and translation is completed. -u Guarantee that any output is unbuffered. In the first synopsis form, the characters in string1 are translated into the characters in string2 where the first character in string1 is translated into the first character in string2 and so on. If string1 is longer than string2, the last character found in string2 is dupli- cated until string1 is exhausted. In the second synopsis form, the characters in string1 are deleted from the input. In the third synopsis form, the characters in string1 are compressed as described for the -s option. In the fourth synopsis form, the characters in string1 are deleted from the input, and the characters in string2 are compressed as described for the -s option. The following conventions can be used in string1 and string2 to specify sets of characters: character Any character not described by one of the following conventions represents itself. octal A backslash followed by 1, 2 or 3 octal digits represents a character with that encoded value. To follow an octal sequence with a digit as a character, left zero-pad the octal sequence to the full 3 octal digits. character A backslash followed by certain special characters maps to special values. a <alert character>  <backspace> f <form-feed> <newline> <carriage return> <tab> v <vertical tab> A backslash followed by any other character maps to that character. c-c Represents the range of characters between the range endpoints, inclusively. [:class:] Represents all characters belonging to the defined character class. Class names are: alnum <alphanumeric characters> alpha <alphabetic characters> cntrl <control characters> digit <numeric characters> graph <graphic characters> lower <lower-case alphabetic characters> print <printable characters> punct <punctuation characters> space <space characters> upper <upper-case characters> xdigit <hexadecimal characters> With the exception of the ``upper'' and ``lower'' classes, characters in the classes are in unspecified order. In the ``upper'' and ``lower'' classes, characters are entered in ascending order. For specific information as to which ASCII characters are included in these classes, see ctype(3) and related manual pages. [=equiv=] Represents all characters belonging to the same equivalence class as equiv, ordered by their encoded values. [#*n] Represents n repeated occurrences of the character represented by #. This expression is only valid when it occurs in string2. If n is omitted or is zero, it is be interpreted as large enough to extend string2 sequence to the length of string1. If n has a leading zero, it is interpreted as an octal value, otherwise, it's interpreted as a decimal value. ENVIRONMENT
The LANG, LC_ALL, LC_CTYPE and LC_COLLATE environment variables affect the execution of tr as described in environ(7). EXAMPLES
The following examples are shown as given to the shell: Create a list of the words in file1, one per line, where a word is taken to be a maximal string of letters. tr -cs "[:alpha:]" " " < file1 Translate the contents of file1 to upper-case. tr "[:lower:]" "[:upper:]" < file1 Strip out non-printable characters from file1. tr -cd "[:print:]" < file1 Remove diacritical marks from all accented variants of the letter 'e': tr "[=e=]" "e" DIAGNOSTICS
The tr utility exits 0 on success, and >0 if an error occurs. COMPATIBILITY
System V has historically implemented character ranges using the syntax ``[c-c]'' instead of the ``c-c'' used by historic BSD implementations and standardized by POSIX. System V shell scripts should work under this implementation as long as the range is intended to map in another range, i.e. the command ``tr [a-z] [A-Z]'' will work as it will map the ``['' character in string1 to the ``['' character in string2. How- ever, if the shell script is deleting or squeezing characters as in the command ``tr -d [a-z]'', the characters ``['' and ``]'' will be included in the deletion or compression list which would not have happened under an historic System V implementation. Additionally, any scripts that depended on the sequence ``a-z'' to represent the three characters ``a'', ``-'' and ``z'' will have to be rewritten as ``a-z''. The tr utility has historically not permitted the manipulation of NUL bytes in its input and, additionally, stripped NUL's from its input stream. This implementation has removed this behavior as a bug. The tr utility has historically been extremely forgiving of syntax errors, for example, the -c and -s options were ignored unless two strings were specified. This implementation will not permit illegal syntax. STANDARDS
The tr utility conforms to IEEE Std 1003.1-2001 (``POSIX.1''). It should be noted that the feature wherein the last character of string2 is duplicated if string2 has less characters than string1 is per- mitted by POSIX but is not required. Shell scripts attempting to be portable to other POSIX systems should use the ``[#*]'' convention instead of relying on this behavior. The -u option is an extension to the IEEE Std 1003.1-2001 (``POSIX.1'') standard. BSD
October 11, 1997 BSD
All times are GMT -4. The time now is 04:11 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy