Solved

Truncate last 40 chars of every line in a file

Posted on 1998-12-02
3
228 Views
Last Modified: 2010-04-21
I am wondering if there is some function to turncate the last 40 chars of every line in a given file. And also, when I execute the system command like "system(". .profile");" in a C program, the profile is not executed.
0
Comment
Question by:bujji120198
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 51

Accepted Solution

by:
ahoffmann earned 30 total points
ID: 2008191
awk '{l=length($0);if(l>40){print substr($0,1,l-40)}}' file

How do you proove that .profile was not executed?
I suggest adding following to your .profile:
echo "called at `date` by $user" > /tmp.profile

Then tell us what the error is, or what does not work.
0
 
LVL 1

Expert Comment

by:toTo
ID: 2008192
another suggestion

"system(". ./.profile");"
0
 
LVL 3

Expert Comment

by:mliberi
ID: 2008193
about "truncating the first 40 characters":
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Do you need a C or shell function?

if C you can use strcpy()
if shell I suggest to use the 'typeset' internal command.

Example (ksh):
let's suppose we want to get the first 40 chars from var 'line'

typeset -L40 L40=$line

echo "$L40"   # first 40 chars, eventually padded with blanks

about "system(". .profile")":
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

I agree with ahoffmann, your .profile should get executed, but
if your purpose was to define and/or export variables, functions,
aliases and so on for sure you failed!

In fact when you call the 'system' C library function your process forks a shell that executes the command ". .profile". All the variables you define there remains in the subprocess environment and are lost when the subprocess exit, just before the 'system' function returns.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tech tip describes how to install the Solaris Operating System from a tape backup that was created using the Solaris flash archive utility. I have used this procedure on the Solaris 8 and 9 OS, and it shoudl also work well on the Solaris 10 rel…
Installing FreeBSD… FreeBSD is a darling of an operating system. The stability and usability make it a clear choice for servers and desktops (for the cunning). Savvy?  The Ports collection makes available every popular FOSS application and packag…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question