Sponsored Content
Full Discussion: Sed append newline character
Top Forums Shell Programming and Scripting Sed append newline character Post 302209136 by chihung on Thursday 26th of June 2008 03:19:09 AM
Old 06-26-2008
Below code is able to solve your problem. I create tmp file which define the mapping array to be included in the BEGIN block in awk and it will be dynamically included in the second awk command to replace the 3rd field based on the mapping.

Code:
$ cat a.sh
#! /bin/sh

if [ $# -ne 2 ]; then
        echo "Usage: $0 <input.txt> <info.txt>"
        exit 1
fi


tmpfile=".tmpfile-$$"
trap "rm -f $tmpfile" 0 1 2 3 15


awk '
{
        printf("map[%d]=\"%s\";",$2,$3)
}' $2 > $tmpfile


awk '
BEGIN {FS=";";OFS=";";'`cat $tmpfile`'}
{
        print $1,$2,map[$3],$4
}' $1

$ ./a.sh input.txt info.txt
test-1;1;Pass;100Mb/s
test-2;1;Fail;
test-1;2;Fail;
test-2;2;Unknown;
test-2;3;Crash;

 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

append newline to files with incomplete last line

Hi all, Is there any way I can check a file for the linefeed character at the end of the file, and append one only if it is missing (ie. Incomplete last line)? Need to do this because I need to write a script to process files FTP-ed over from various machines, which may or may not be... (1 Reply)
Discussion started by: ziyi
1 Replies

2. Shell Programming and Scripting

Using SED to append character to each line

Hey - my first post here, and I'm a total SED newb. I've looked around for previous help on this, but have so far been unsuccessful. I have a program (AMStracker for OS X) that outputs data in the terminal. Output is in this form: . . . 3 0 -75 3 0 -76 3 0 -77 ... (4 Replies)
Discussion started by: c0nn0r
4 Replies

3. Shell Programming and Scripting

append newline to existing variables

firstly, i check is the variable empty or not, if so vesselNameList=`echo $vesselName` if not vesselNameList="${vesselNameList}""\n"`echo "$vesselName"` and it produces this result BUNGA TERATAI 3 5055\ JADE TRADER 143W\ MOL SPLENDOR 0307A BUNGA TERATAI 3 5055\ JADE... (1 Reply)
Discussion started by: finalight
1 Replies

4. Shell Programming and Scripting

Append newline at the file end

Hi All, Is there any way to append a newline character at the end of a file(coma-separated file), through shell script? I need to check whether newline character exists at the end of a file, if it does not then append it. Regards, Krishna (1 Reply)
Discussion started by: KrishnaSaran
1 Replies

5. UNIX for Dummies Questions & Answers

newline character

hi, I want to print the below lines "Message from bac logistics The Confirmation File has not been received." When i give like this in the code "Message from bac logistics\n The Confirmation File has not been received." It is giving only Message from bac logistics\n The... (9 Replies)
Discussion started by: trichyselva
9 Replies

6. Shell Programming and Scripting

any savant ? using AWK/SED to remove newline character between two strings : conditional removal

I'd like to remove (do a pattern or precise replacement - this I can handle in SED using Regex ) ---AFTER THE 1ST Occurrence ( i.e. on the 2nd occurrence - from the 2nd to fourth occurance ) of a specific string : type 1 -- After the 1st occurrence of 1 string1 till the 1st occurrence of... (4 Replies)
Discussion started by: sieger007
4 Replies

7. Shell Programming and Scripting

Why SED can't see the last newline character?

Removed. My question does not make sense. and SED does see the last newline character. But I still have a question: How to remove the last newline character(the newline character at the end of last line) using SED? ---------- Post updated 05-01-11 at 10:51 AM ---------- Previous update was... (7 Replies)
Discussion started by: kevintse
7 Replies

8. UNIX for Dummies Questions & Answers

newline character in a variable

variable="unix\nlinux" echo $variable expected output: unix linux :wall: can i do that ?? thanks in advance!! (3 Replies)
Discussion started by: sathish92
3 Replies

9. Shell Programming and Scripting

How to remove newline character if it is the only character in the entire file.?

I have a file which comes every day and the file data look's as below. Vi abc.txt a|b|c|d\n a|g|h|j\n Some times we receive the file with only a new line character in the file like vi abc.txt \n (8 Replies)
Discussion started by: rak Kundra
8 Replies

10. UNIX for Beginners Questions & Answers

Replace newline with comma and append quotes

Hi, I have below requirement. Apple Orange Banana Required O/p in bash 'Apple,Orange,Banana' Can you please help. Please wrap your samples, codes in CODE TAGS as per forum rules. (3 Replies)
Discussion started by: Rtk
3 Replies
tmpfile(3S)															       tmpfile(3S)

NAME
tmpfile() - create a temporary file SYNOPSIS
DESCRIPTION
creates a temporary file by generating a name through (see tmpnam(3S)), and returns a corresponding pointer. If the file cannot be opened a NULL pointer is returned. The file is automatically deleted when the process using it terminates. The file is opened for update Notes On HP-UX systems, the mode is equivalent to the mode. ERRORS
The function will fail if: [EOVERFLOW] The named file is a regular file and the size of the file cannot be represented correctly in an object of size in this environment. Additional values may be set by the underlying function (see fopen(3S)). SEE ALSO
creat(2), unlink(2), mktemp(3C), fopen(3S), fgetpos64(3S), tmpnam(3S), thread_safety(5). STANDARDS CONFORMANCE
tmpfile(3S)
All times are GMT -4. The time now is 04:12 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy