Sponsored Content
Top Forums Shell Programming and Scripting Suppress new line at end here string? Post 302913048 by Don Cragun on Wednesday 13th of August 2014 01:30:11 PM
Old 08-13-2014
The read -n num_chars option is an extension to the standards that overrides the default behavior. Try that again without the -n1, and print using strings and you'll get what I suggested:
Code:
( while read -r a ;do printf "%s" "$a"|od -c;done;) < file

producing:
Code:
0000000    1                                                            
0000001
0000000    2                                                            
0000001
0000000    3                                                            
0000001

(I used od -c instead of hd because hd isn't available on my system.)

When invoked as:
Code:
read -r var...

the current standards describe the requirements as:
Quote:
The terminating <newline> (if any) shall be removed from the input and the results shall be split into fields as in the shell for the results of parameter expansion (see Section 2.6.5, on page 2333); the first field shall be assigned to the first variable var, the second field to the second variable var, and so on. If there are fewer fields than there are var operands, the remaining vars shall be set to empty strings. If there are fewer vars than fields, the last var shall be set to a value comprising the following elements:
  • The field that corresponds to the last var in the normal assignment sequence described above
  • The delimiter(s) that follow the field corresponding to the last var
  • The remaining fields and their delimiters, with trailing IFS white space ignored
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

To cut end string from line

HI, I want to cut end string from line. e.g. i have following input line /users/home/test.txt I want to get end string 'test.txt' from above line and length of that end string will change and it always start after '/'. Thanks, Visu (7 Replies)
Discussion started by: visu
7 Replies

2. UNIX for Dummies Questions & Answers

HOWTO Append String to End of Line

I have a comma delimited text file and need to appened ",000000" to the end of every line. For example: Before: "D700000","2006" ,"5000","Open Year" ,"Conversion" ,"Wk64","Productive Payroll $" ,1103.45 After: "D700000","2006" ,"5000","Open Year" ,"Conversion" ,"Wk64","Productive Payroll... (3 Replies)
Discussion started by: bggibson
3 Replies

3. Shell Programming and Scripting

How can I append a string at the end of a line in a file

Hi, guys. I have one question: I have a file called "group", the contents of it is below: ******************************** ... test:x:203: sales:x:204: repair:x:205: research:x:206:brownj ... *********** Now I want to add string ",sherrys" at the end of "research:x:206:brownj", so... (5 Replies)
Discussion started by: daikeyang
5 Replies

4. Shell Programming and Scripting

Add string end of line

Hello How can I add a string (always the same) at the end of a specific line in a file... The file is: 000000001 041 L $$aspa 000000001 088 L $$aJ.E.N. 551 000000001 090 L $$aINFORMES JEN 000000001 100 L $$aautor 1 ---- 000000002 041 L $$aeng 000000002 088 L $$aJ.E.N. 1... (13 Replies)
Discussion started by: ldiaz2106
13 Replies

5. Shell Programming and Scripting

put string end of the line

I've a problem to put .h end of the line..below my input file fg_a bb fg_b bb fg_c bb fg_d aa fg_f ee and i want the output file as below fg_a.h bb fg_b.h bb fg_c.h bb fg_d.h (6 Replies)
Discussion started by: zulabc
6 Replies

6. Shell Programming and Scripting

Search a string in a text file and add another string at the end of line

Dear All I am having a text file which is having more than 200 lines. EX: 001010122 12000 BIB 12000 11200 1200003 001010122 2000 AND 12000 11200 1200003 001010122 12000 KVB 12000 11200 1200003 In the above file i want to search for string KVB... (5 Replies)
Discussion started by: suryanarayana
5 Replies

7. Shell Programming and Scripting

Append this string to end of each line

Platform: Solaris 10 I have a file like below $ cat languages.txt Spanish Norwegian English Persian German Portugese Chinese Korean Hindi Malayalam Bengali Italian Greek Arabic I want to append the string " is a great language" at end of each line in this file. (3 Replies)
Discussion started by: omega3
3 Replies

8. Shell Programming and Scripting

How to extract text from STRING to end of line?

Hi I have a very large data file with several hundred columns and millions of lines. The important data is in the last set of columns with variable numbers of tab delimited fields in front of it on each line. Im currently trying sed to get the data out - I want anything beetween :RES and... (4 Replies)
Discussion started by: Manchesterpaul
4 Replies

9. Red Hat

How to add a new string at the end of line by searching a string on the same line?

Hi, I have a file which is an extract of jil codes of all autosys jobs in our server. Sample jil code: ************************** permission:gx,wx date_conditions:yes days_of_week:all start_times:"05:00" condition: notrunning(appDev#box#ProductLoad)... (1 Reply)
Discussion started by: raghavendra
1 Replies

10. Shell Programming and Scripting

Printing string from last field of the nth line of file to start (or end) of each line (awk I think)

My file (the output of an experiment) starts off looking like this, _____________________________________________________________ Subjects incorporated to date: 001 Data file started on machine PKSHS260-05CP ********************************************************************** Subject 1,... (9 Replies)
Discussion started by: samonl
9 Replies
READ(P) 						     POSIX Programmer's Manual							   READ(P)

NAME
read - read a line from standard input SYNOPSIS
read [-r] var... DESCRIPTION
The read utility shall read a single line from standard input. By default, unless the -r option is specified, backslash ( '' ) shall act as an escape character, as described in Escape Character (Back- slash) . If standard input is a terminal device and the invoking shell is interactive, read shall prompt for a continuation line when: * The shell reads an input line ending with a backslash, unless the -r option is specified. * A here-document is not terminated after a <newline> is entered. The line shall be split into fields as in the shell (see Field Splitting ); the first field shall be assigned to the first variable var, the second field to the second variable var, and so on. If there are fewer var operands specified than there are fields, the leftover fields and their intervening separators shall be assigned to the last var. If there are fewer fields than vars, the remaining vars shall be set to empty strings. The setting of variables specified by the var operands shall affect the current shell execution environment; see Shell Execution Environ- ment . If it is called in a subshell or separate utility execution environment, such as one of the following: (read foo) nohup read ... find . -exec read ... ; it shall not affect the shell variables in the caller's environment. OPTIONS
The read utility shall conform to the Base Definitions volume of IEEE Std 1003.1-2001, Section 12.2, Utility Syntax Guidelines. The following option is supported: -r Do not treat a backslash character in any special way. Consider each backslash to be part of the input line. OPERANDS
The following operand shall be supported: var The name of an existing or nonexisting shell variable. STDIN
The standard input shall be a text file. INPUT FILES
None. ENVIRONMENT VARIABLES
The following environment variables shall affect the execution of read: IFS Determine the internal field separators used to delimit fields; see Shell Variables . LANG Provide a default value for the internationalization variables that are unset or null. (See the Base Definitions volume of IEEE Std 1003.1-2001, Section 8.2, Internationalization Variables for the precedence of internationalization variables used to determine the values of locale categories.) LC_ALL If set to a non-empty string value, override the values of all the other internationalization variables. LC_CTYPE Determine the locale for the interpretation of sequences of bytes of text data as characters (for example, single-byte as opposed to multi-byte characters in arguments). LC_MESSAGES Determine the locale that should be used to affect the format and contents of diagnostic messages written to standard error. NLSPATH Determine the location of message catalogs for the processing of LC_MESSAGES . PS2 Provide the prompt string that an interactive shell shall write to standard error when a line ending with a backslash is read and the -r option was not specified, or if a here-document is not terminated after a <newline> is entered. ASYNCHRONOUS EVENTS
Default. STDOUT
Not used. STDERR
The standard error shall be used for diagnostic messages and prompts for continued input. OUTPUT FILES
None. EXTENDED DESCRIPTION
None. EXIT STATUS
The following exit values shall be returned: 0 Successful completion. >0 End-of-file was detected or an error occurred. CONSEQUENCES OF ERRORS
Default. The following sections are informative. APPLICATION USAGE
The -r option is included to enable read to subsume the purpose of the line utility, which is not included in IEEE Std 1003.1-2001. The results are undefined if an end-of-file is detected following a backslash at the end of a line when -r is not specified. EXAMPLES
The following command: while read -r xx yy do printf "%s %s " "$yy" "$xx" done < input_file prints a file with the first field of each line moved to the end of the line. RATIONALE
The read utility historically has been a shell built-in. It was separated off into its own utility to take advantage of the richer descrip- tion of functionality introduced by this volume of IEEE Std 1003.1-2001. Since read affects the current shell execution environment, it is generally provided as a shell regular built-in. If it is called in a sub- shell or separate utility execution environment, such as one of the following: (read foo) nohup read ... find . -exec read ... ; it does not affect the shell variables in the environment of the caller. FUTURE DIRECTIONS
None. SEE ALSO
Shell Command Language COPYRIGHT
Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1, 2003 Edition, Standard for Information Technol- ogy -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 6, Copyright (C) 2001-2003 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html . IEEE
/The Open Group 2003 READ(P)
All times are GMT -4. The time now is 03:26 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy